【问题标题】:How to stop Avaudiosession from deactivating?如何阻止 Avaudiosession 停用?
【发布时间】:2016-05-09 19:10:09
【问题描述】:

我想通过扬声器播放歌曲,同时能够使用 Quickblox 接听视频通话。

我的音频速率变得一团糟。 还有一个更大的问题是当通话结束时,quickblox 框架将音频会话设置为停用状态。 IE [Avaudiosession sharedInstance]setActive:NO....

如何阻止这种情况发生?

或者有没有办法处理上面提到的场景。??

我已经通过谷歌阅读了一个月,但仍然没有找到任何合适的答案或任何指导方针。 谁能帮我解决这个问题/??

【问题讨论】:

    标签: ios objective-c voip quickblox avaudiosession


    【解决方案1】:

    首先,要让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;
        }
    }
    

    【讨论】:

    • 很好的答案!但遗憾的是没有解决问题。我检查了我收到此错误的日志: AVAudioSession.mm:692: -[AVAudioSession setActive:withOptions:error:]: Deactivating an audio session that has running I/O.在停用音频会话之前,应停止或暂停所有 I/O。还有:AVAudioSession.mm:692:-[AVAudioSession setActive:withOptions:error:]: 停用正在运行 I/O 的音频会话。在停用音频会话之前,应停止或暂停所有 I/O。
    • @saurabh2810 上面的示例用于处理来自其他应用程序的中断。您收到正在运行的 I/O 错误,因为 SAME 应用程序的其他部分正在使用 AV。检查您的课程或混合您的插件是否可能存在争用。
    猜你喜欢
    • 1970-01-01
    • 2014-03-20
    • 2015-01-08
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 2015-08-13
    相关资源
    最近更新 更多