应用程序重新激活后,AVAudioSession无法播放声音

时间:2014-10-04 19:46:52

标签: ios core-audio avaudiosession

所以我有一个使用AVAudioSession的音乐应用程序,允许它在后台播放。我用这个电话:

[audioSession setActive:YES
            withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation
                  error:nil];

我现在的问题是,如果我去另一个应用程序,它会窃取音频会话(因此现在停止从我的应用程序播放音乐并播放其他内容),我回到我的应用程序,无论我做什么来重置我的音频会话或我的音频单元,我的应用程序的声音消失了。

有谁知道该怎么做?

1 个答案:

答案 0 :(得分:5)

因此,在注册AVAudioSession通知后:

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

你需要恢复/重启你需要在处理程序中断类型AVAudioSessionInterruptionTypeEnded重启你的播放器:

- (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
        } break;
        case AVAudioSessionInterruptionTypeEnded:{
            // • Make session active
            // • Update user interface
            // • AVAudioSessionInterruptionOptionShouldResume option
            if (interruptionOption.unsignedIntegerValue == AVAudioSessionInterruptionOptionShouldResume) {
                // Here you should continue playback.
                [player play];
            }
        } break;
        default:
            break;
    }
}

您可以在此处查看完整说明:AVplayer resuming after incoming call