为什么iOS不通知我的应用程序音频会话中断?

mih*_*iho 2 objective-c ios avaudiosession

我正在使用AVPlayer播放来自不同来源(包括iPod音乐库)的声音.由于AVPlayer是更低级别的AVAudioPlayer,我必须自己处理中断.使用AVAudioPlayer不是一个选项!

在Apple开发者文档中,他们提到要么倾听,AVAudioSessionInterruptionNotification要么设置一个监听器AudioSessionInitialize.但是当这样做时我只在中断结束时收到通知,但由于他们的文件,我的应用程序应该能够处理两者.

我使用以下代码初始化我的音频会话:(简化版,删除不重要的行)

AudioSessionInitialize(NULL, NULL, ARInterruptionListenerCallback, nil);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
AudioSessionSetActive(true);
Run Code Online (Sandbox Code Playgroud)

以下是听众的样子:

void ARInterruptionListenerCallback(void *inUserData, UInt32 interruptionState) {
    if (interruptionState == kAudioSessionBeginInterruption) {
        // Here is the code which is never called...
    }
    else if (interruptionState == kAudioSessionEndInterruption) {
        // But this case will be executed!
    }
}
Run Code Online (Sandbox Code Playgroud)

顺便说一句.我希望在电话或类似电话中断时执行此代码.我误解了Apple宣称中断的内容吗?

sam*_*899 5

有一个在iOS的6错误中断代码似乎从来没有被要求开始中断 - 这既包括旧的过时的方法(使用AVAudioSession代表)和新的通知方法.

我自己和其他一些人已经向Apple提出了严重错误请求,所以我建议你这样做(bugreport.apple.com)以引起他们的注意.

我不确定你正在使用什么中断,但我正在使用它来在必要时在中断后恢复音频,并且还更新界面中的播放/暂停按钮.我不得不改变我的方法来观看AVPlayer rate属性以切换播放/暂停按钮,并且在中断后不允许音频恢复.

编辑:看到这个帖子