アプリ内で音楽再生や録音中に通話やアラーム、Siriなどによる割り込みがあった場合の制御方法です。
これを行う事でAudioPlayerで音楽の再生中に割り込みが発生した後に、再度再生させるなどの処理を実装することが可能です。
実装方法
以下のNotificationを登録することで、割り込み時に通知を受け取る事ができます。
// 割り込みが発生した場合の通知を登録
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(audioInterrupted:)
name:AVAudioSessionInterruptionNotification
object:nil];
割り込み発生時の処理内で、割り込み開始か終了かを取得し処理を行います。
割り込みの開始/終了はAVAudioSessionInterruptionTypeKeyから取得することができます。
- (void)audioInterrupted:(NSNotification*) notification{
NSNumber *interruptType = [notification.userInfo objectForKey:@"AVAudioSessionInterruptionTypeKey"];
if ([interruptType unsignedIntegerValue] == AVAudioSessionInterruptionTypeBegan){
//割り込み開始
}else if([interruptType unsignedIntegerValue] == AVAudioSessionInterruptionTypeEnded){
//割り込み終了
}
}