我添加了通知来听取其他人的打扰:
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(audioInterruption:)
name:AVAudioSessionInterruptionNotification
object:nil];
- (void)audioInterruption:(NSNotification *)audioInterruptNotification{
NSLog(@"interrupted!");
NSDictionary *interruptionDictionary = [audioInterruptNotification userInfo];
NSNumber *interruptionType = (NSNumber *)[interruptionDictionary valueForKey:AVAudioSessionInterruptionTypeKey];
if ([interruptionType intValue] == AVAudioSessionInterruptionTypeBegan) {
NSLog(@"Interruption started");
} else if ([interruptionType intValue] == AVAudioSessionInterruptionTypeEnded){
NSLog(@"Interruption ended");
BOOL success = [[AVAudioSession sharedInstance] setActive:YES withOptions:NO error:nil];
if (!success) {
NSLog(@"avaudio session set active failed");
}
} else {
NSLog(@"Something else happened");
}
}
当发生中断时,通知很好但是没有
[[AVAudioSession sharedInstance] setActive:YES withOptions:NO error:nil];
。那么为什么会发生这种情况,如何在中断后恢复播放(我使用AudioQueue播放或录制而不是AVAudioPlayer)?