2012-10-07 24 views
2

我使用AVPlayer播放來自不同來源(包括iPod音樂庫)的聲音。由於AVPlayer是更低級別的AVAudioPlayer,我必須自己處理中斷。使用AVAudioPlayer不是一種選擇!爲什麼iOS不通知我的應用程序音頻會話中斷?

在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! 
    } 
} 

順便說一句。我希望在有電話或類似的中斷時執行此代碼。我誤解了蘋果宣佈的中斷?

回答

5

iOS 6中存在一個錯誤。中斷代碼似乎永遠不會被稱爲開始中斷 - 包括舊的棄用方法(使用AVAudioSession委託)和新的通知方法。

我自己和其他一些人已經向蘋果公司提出了一個高嚴重性的bug請求,所以我建議你也這樣做(bugreport.apple.com)以引起他們的注意。

我不確定你在用什麼中斷,但是我用它來允許在中斷後恢復音頻(如果有必要的話),並更新界面中的播放/暫停按鈕。我不得不改變我的方法來觀看AVPlayer rate屬性來切換播放/暫停按鈕,並且不允許音頻在中斷後恢復。

編輯:看到this thread

+0

感謝您的答覆。我也創造了一個這樣的工作。我現在要報告這個問題。 – miho

相關問題