iOS下微信語音播放之切換聽筒和揚聲器的方法解決方案

小涵發表於2015-07-23

[[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];

        }

    }

}

 
 
 
注意事項(也就是我說的問題)
    對於不希望啟動接近感測器功能的應用,如果需要進行揚聲器和聽筒進行切換過程中,則必須通過啟用接近感測器來進行聲音輸出模式的切換,在此時,必須要注意,如果當聲音通過聽筒進行播放完畢時,在播放完畢時,此時仍在聽筒模式輸出,如果此時關閉感測器功能,則導致在離開聽筒時,由於感測器功能已經關閉,應用無法再次收到註冊的感測器變更通知,而此時如果未能將底層的聲音輸出模式切換,則導致相關的聲音輸出仍從聽筒中輸出,即使引起感測器反映的障礙已經離開感測器作用範圍,但應用中獲取的感測器狀態仍未接近狀態,使根據感測器狀態進行切換聲音輸出模式操作失效。 
    特殊情況:
在iPhone 4s及iPhone5中,在接近感測器功能關閉後,如果此時感測器狀態為YES,則在再次啟動聲音感測器時,不會收到感測器的變更通知;
在iPhone 4中,在接近感測器功能關閉後,如果此時感測器狀態為YES,則在再次啟動聲音感測器時,會先收到一次感測器的變更通知;
   此問題的解決方案:當在感測器功能開始時,如果此時感測器感測狀態為YES時,此時聲音播放結束,仍未出發感測器狀態變更時,此時不關閉感測器功能。當引起感測器反映的障礙已經離開感測器作用範圍,此時會收到感測器變更通知,在變更通知中檢測當前感測器狀態是否為開啟狀態及聲音播放狀態,如果在感測器狀態為YES時,而此時需要開啟感測器功能的操作(如聲音播放功能)已經結束時,則將感測器功能關閉即可;
 
-------也就是說,在不是黑屏的狀態下,關閉近感測器功能。就沒什麼問題了。

相關文章