我正在使用AVPlayer播放来自不同来源(包括iPod音乐库)的声音。由于AVPlayer是更低级别的AVAudioPlayer,我必须自己处理中断。使用AVAudioPlayer不是一个选项!
在Apple开发者文档中,他们提到要么AVAudioSessionInterruptionNotification
,要么设置一个AudioSessionInitialize
的监听器。但在这样做时,我仅在中断结束时收到通知,但由于their documents我的应用应该能够同时处理。
我正在使用以下代码初始化我的音频会话:(简化版,删除不重要的行)
AudioSessionInitialize(NULL, NULL, ARInterruptionListenerCallback, nil);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
AudioSessionSetActive(true);
以下是听众的样子:
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!
}
}
顺便说一下。我希望在电话或类似电话中断时执行此代码。我误解了Apple宣称中断的原因吗?
答案 0 :(得分:5)
iOS 6中存在一个错误。中断代码似乎永远不会被调用为开始中断 - 这包括旧的弃用方法(使用AVAudioSession委托)和新的通知方法。
我和其他一些人已经向Apple提出了严重错误请求,所以我建议你这样做(bugreport.apple.com)以引起他们的注意。
我不确定你正在使用什么中断,但我正在使用它来在必要时在中断后恢复音频,并且还更新界面中的播放/暂停按钮。我不得不改变我的方法来观看AVPlayer rate属性以切换播放/暂停按钮,并且在中断后不允许音频恢复。
修改:请参阅this thread