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

[[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: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時,而此時需要開啓傳感器功能的操作(如聲音播放功能)已經結束時,則將傳感器功能關閉即可;

-------也就是說,在不是黑屏的狀態下,關閉近傳感器功能。就沒什麼問題了。

手動切換兩種模式
解決方案:添加長按手勢,切換爲另一種模式。
代碼片段:

UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizeralloc] initWithTarget:self

action:@selector(longPressed:)];

    [longPressGestureRecognizersetMinimumPressDuration:1.0f];

    [longPressGestureRecognizersetAllowableMovement:50.0];

    [self.bubbleBgImageViewaddGestureRecognizer:longPressGestureRecognizer];

    [longPressGestureRecognizerrelease];


---------

-(void)longPressed:(UILongPressGestureRecognizer *) gestureRecognizer

{

   switch (gestureRecognizer.state)

    {

        caseUIGestureRecognizerStateEnded:

            

           break;

        caseUIGestureRecognizerStateCancelled:

            

           break;

        caseUIGestureRecognizerStateFailed:

            

           break;

        caseUIGestureRecognizerStateBegan:

           if ([self.voiceDelegaterespondsToSelector:@selector(BaseChartVoiceLongPressed)])

            {

                [self.voiceDelegateBaseChartVoiceLongPressed];

            }


           break;

        caseUIGestureRecognizerStateChanged:

            

           break;

       default:

           break;

    }

    }


-------------

#pragma mark BaseChartCellDelegate

-(void)BaseChartVoiceLongPressed

{

    NSLog(@"voice long Pressed");

    

    if ([[[AVAudioSessionsharedInstance]category] isEqualToString:AVAudioSessionCategoryPlayback])

    {

        //切換爲聽筒播放

        [[AVAudioSessionsharedInstance]setCategory:AVAudioSessionCategoryPlayAndRecorderror:nil];

        [selfshowTipInfo:@"切換爲聽筒模式"];

        

    }

   else

    {

        //切換爲揚聲器播放

        [[AVAudioSessionsharedInstance]setCategory:AVAudioSessionCategoryPlaybackerror:nil];

        [selfshowTipInfo:@"切換爲揚聲器模式"];

    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章