[[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; //建議在播放之前設置yes,播放結束設置NO,這個功能是開啟紅外感應
//添加監聽
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(sensorStateChange:)
name:@"UIDeviceProximityStateDidChangeNotification"
object:nil];
//處理監聽觸發事件
-(void)sensorStateChange:(NSNotificationCenter *)notification;
{
//如果此時手機靠近面部放在耳朵旁,那么聲音將通過聽筒輸出,並將屏幕變暗(省電啊)
if ([[UIDevice currentDevice] proximityState] == YES)
{
NSLog(@"Device is close to user");
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
}
else
{
NSLog(@"Device is not close to user");
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
}
}
//初始化播放器的時候如下設置
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
sizeof(sessionCategory),
&sessionCategory);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
sizeof (audioRouteOverride),
&audioRouteOverride);
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
//默認情況下揚聲器播放
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:YES error:nil];
在 iOS 中,並非所有 iOS 設備都擁有近距離傳感器。這里介紹如何調用 iPhone 的距離傳感器。
使用近距離傳感器
UIDevice 中有兩個近距離傳感器的屬性:proximityMonitoringEnabled 和 proximityState。這兩個屬性都是 iOS 3.0 及以上才支持的。
proximityMonitoringEnabled 屬性
To determine if proximity monitoring is available, attempt to enable it. If the value of the proximityState property remains NO, proximity monitoring is not available.
要確定近距離傳感器是否可用,可以嘗試啟用它,即 proximityMonitoringEnabled = YES,如果設置的屬性值仍然為NO,說明傳感器不可用。
proximityState 屬性
傳感器已啟動前提條件下,如果用戶接近 近距離傳感器,此時屬性值為YES,並且屏幕已關閉(非休眠)。And vice versa。
Notification
UIDeviceProximityStateDidChangeNotification,當近距離傳感器狀態改變時發生。
//添加近距離事件監聽,添加前先設置為YES,如果設置完后還是NO的讀話,說明當前設備沒有近距離傳感器
[[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
if ([UIDevice currentDevice].proximityMonitoringEnabled == YES) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sensorStateChange:)name:UIDeviceProximityStateDidChangeNotification object:nil];
}
[[UIDevice currentDevice] setProximityMonitoringEnabled:NO];
[[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
if ([UIDevice currentDevice].proximityMonitoringEnabled == YES) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceProximityStateDidChangeNotification object:nil];
}
[[UIDevice currentDevice] setProximityMonitoringEnabled:NO];
#pragma mark - 處理近距離監聽觸發事件
-(void)sensorStateChange:(NSNotificationCenter *)notification;
{
//如果此時手機靠近面部放在耳朵旁,那么聲音將通過聽筒輸出,並將屏幕變暗(省電啊)
if ([[UIDevice currentDevice] proximityState] == YES)//黑屏
{
NSLog(@"Device is close to user");
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
}
else//沒黑屏幕
{
NSLog(@"Device is not close to user");
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
if (![MTool isPlayRecodering]) {//沒有播放了,也沒有在黑屏狀態下,就可以把距離傳感器關了
[[UIDevice currentDevice] setProximityMonitoringEnabled:NO];
}
}
}