1.设置监听,设置红外线开启状态(先关闭)
//监听是否靠近耳朵
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sensorStateChange:) name:@"UIDeviceProximityStateDidChangeNotification" object:nil];
[[UIDevice currentDevice] setProximityMonitoringEnabled:NO]; //开启红外感应
2.监听函数
#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];
}
}