簡體   English   中英

如何阻止Avaudiosession停用?

[英]How to stop Avaudiosession from deactivating?

我想通過揚聲器播放歌曲,同時能夠使用Quickblox接收視頻通話。

我的音頻速率混亂了。 還有一個更大的問題是,當調用結束時quickblox框架將音頻會話設置為停用狀態。 即[Avaudiosession sharedInstance] setActive:NO ....

如何阻止這種情況的發生?

還是有辦法解決上述情況??

我已經閱讀了一個月的google,但仍然找不到任何合適的答案或指南。 誰能幫我解決這個問題/?

首先,要允許AVAudioSession與其他AVAudioSessions ,您需要在初始化時設置選項AVAudioSessionCategoryOptionMixWithOthers

NSError *sessionError = NULL;
    BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
                                                    withOptions:AVAudioSessionCategoryOptionMixWithOthers
                                                          error:&sessionError];

    if(!success) {
        NSLog(@"Error setting category Audio Session: %@", [sessionError localizedDescription]);
    }

要處理中斷(呼叫,警報等),您應該為NSNotificationCenter設置一個中斷觀察器,必要時可以在該處處理AVAudioSession的激活/停用:

[[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(handleAudioSessionInterruption:)
                                                     name:AVAudioSessionInterruptionNotification
                                                   object:[AVAudioSession sharedInstance]];

NSNotification將帶有中斷類型和密鑰:

- (void)handleAudioSessionInterruption:(NSNotification*)notification {

    NSNumber *interruptionType = [[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey];
    NSNumber *interruptionOption = [[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey];

    switch (interruptionType.unsignedIntegerValue) {
        case AVAudioSessionInterruptionTypeBegan:{
            // • Audio has stopped, already inactive
            // • Change state of UI, etc., to reflect non-playing state
            NSLog(@"AVAudioSessionInterruptionTypeBegan");

        } break;
        case AVAudioSessionInterruptionTypeEnded:{
            // • Make session active
            // • Update user interface
            // • AVAudioSessionInterruptionOptionShouldResume option
            NSLog(@"AVAudioSessionInterruptionTypeEnded");
            if (interruptionOption.unsignedIntegerValue == AVAudioSessionInterruptionOptionShouldResume) {
                // Here you should continue playback.
            }
        } break;
        default:
            break;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM