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);
そして、リスナーは次のようになります。
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が中断と宣言していることを誤解していますか?