iOS 红外感应

红外感应

今天项目需求,当你播放音频的时候,
靠近耳朵的时候,需要把 扬声器(外放) 转为 话筒(内放)
离开耳朵的时候,需要把 话筒(内放) 转为 扬声器(外放)
就跟你打电话的时候,听筒和扬声器的转换一样!

//监听 听筒模式or扬声器模式
//监听是否靠近耳朵
#pragma mark -  开启红外感应   YES开启   NO关闭
    [[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sensorStateChange:) name:@"UIDeviceProximityStateDidChangeNotification" object:nil];

#pragma mark - 监听是否靠近耳朵
-(void)sensorStateChange:(NSNotificationCenter *)notification;
{
    AVAudioSession *session = [AVAudioSession sharedInstance];
    NSError *sessionError;
    if ([[UIDevice currentDevice] proximityState] == YES)
    {
        //靠近耳朵
        [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
    }
    else
    {
        //远离耳朵
        [session setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容