我想知道何时我的AVAudioRecorder
无法访问(例如在播放音乐时)。
由于audioRecorderEndInterruption
将在iOS 9中被弃用,因此我正在关注AVAudioSession
的中断通知(但两者都没有按预期工作)。
问题在于,如果应用程序在发生中断时处于前台并继续保持在前台,则不会调用中断通知。
例如:用户启动和停止播放音乐,而不将应用程序移至后台。
为了检测任何中断,我正在使用:
[[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
(这意味着它只有在将应用程序放入后台并重新回到前台后才会被调用)。
当应用程序在前台发生中断时,我该如何接收InterruptionTypeEnded
通知?