由于audioRecorderEndInterruption将不再适用于iOS 9,因此我专注于AVAudioSession的中断通知(但是两者都无法正常工作).@H_502_3@
问题是如果应用程序在发生中断时仍然处于前台,则不会调用中断通知.@H_502_3@
例如,用户开始并停止播放音乐,而不将应用程序移动到后台.@H_502_3@
检测我使用的任何中断:@H_502_3@
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionWasInterrupted:) name:AVAudioSessionInterruptionNotification object:nil]; ... - (void)audioSessionWasInterrupted:(NSNotification *)notification { if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) { NSLog(@"Interruption notification"); if ([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeBegan]]) { NSLog(@"InterruptionTypeBegan"); } else { NSLog(@"InterruptionTypeEnded"); } } }
我按预期得到InterruptionTypeBegan,但是如果应用程序仍然在前台,则不会调用InterruptionTypeEnded(这意味着在应用程序放置在后台并返回到前台之前不会被调用).@H_502_3@
解决方法
正如在’s documentation关于音频中断的问题所解释的,InterruptionTypeEnded应该实际应用于所提到的场景:@H_502_3@
If the user dismisses the interruption … the system invokes your callback method,indicating that the interruption has ended.@H_502_3@
但是,它也指出,InterruptTypeEnded可能根本不被调用:@H_502_3@
There is no guarantee that a begin interruption will have an end interruption.@H_502_3@
当谈到处理音乐中断时,这个问题不会在很长一段时间. iOS 9有效地防止在调用应用程序的音频处理程序时使用外部音频源.@H_502_3@
处理媒体中断的确切问题的方法可能是听MPMusicPlayerController的playbackState,如此stackoverflow问题:Detecting if music is playing?所示.@H_502_3@
在InterruptionTypeBegan时通过重新调用音频组件来完全阻止音频中断.@H_502_3@