接近感应传感器使用


[[UIDevice currentDevice] setProximityMonitoringEnabled:YES];



-(void)sensorStateChange:(NSNotificationCenter *)notification {
if ([[UIDevice currentDevice] proximityState] == YES) {
NSLog(@"接近");
audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
}else{
NSLog(@"远离");
}
}


AVAudioPlayer,当用户靠近接近感应器时,用听筒播放

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(sensorStateChange:)
name:@"UIDeviceProximityStateDidChangeNotification"
object:nil];


-(void)sensorStateChange:(NSNotificationCenter *)notification {
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
if ([[UIDevice currentDevice] proximityState] == YES) {
audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
}

AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);
}

你可能感兴趣的:(iphone)