自动切换手机耳机模式和话筒模式

来源:互联网 发布:淘宝企业店铺贷款条件 编辑:程序博客网 时间:2024/06/02 10:38


如果此时手机靠近面部放在耳朵旁,那么声音将通过听筒输出,并将屏幕变暗(省电啊)

//处理监听触发事件-(void)sensorStateChange:(NSNotificationCenter *)notification;{    //如果此时手机靠近面部放在耳朵旁,那么声音将通过听筒输出,并将屏幕变暗(省电啊)    if ([[UIDevice currentDevice] proximityState] == YES)    {        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];    }    else    {        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];    }}- (void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    /*======================监听语音=================================*/    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sensorStateChange:)name:@"UIDeviceProximityStateDidChangeNotification"                                               object:nil];    }-(void)viewDidDisappear:(BOOL)animated{    [[NSNotificationCenter defaultCenter]removeObserver:self];}


0 0
原创粉丝点击