今天项目需求,当你播放音频的时候,
靠近耳朵的时候,需要把 扬声器(外放) 转为 话筒(内放)
离开耳朵的时候,需要把 话筒(内放) 转为 扬声器(外放)
就跟你打电话的时候,听筒和扬声器的转换一样!
//监听 听筒模式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];
}
}
网友评论