问题:
我的应用程序的音频在中断(例如电话或使用其他应用程序时)后没有恢复。当我的应用程序在中断发生之前已经处于后台状态时,会发生此问题。
处理中断的代码:
- (void)handleInterruption:(NSNotification *) notification{
if (notification.name != AVAudioSessionInterruptionNotification || notification.userInfo == nil) {
return;
}
NSDictionary *info = notification.userInfo;
if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {
if ([[info valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeBegan]]) {
NSLog(@"InterruptionTypeBegan");
} else {
NSLog(@"InterruptionTypeEnded");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
NSLog(@"playing audio");
self->_audioPlayer.numberOfLoops = -1;
[self->_audioPlayer play];
});
}
}
}
在 Xcode 控制台中,我看到以下序列的日志,但没有播放音频:
- 中断类型开始
- 中断类型结束
- 播放音频
我将不胜感激有关此主题的任何建议和想法。谢谢你。
编辑:
我注意到许多在线音乐流媒体应用程序(例如 Saavn 和 Spotify)存在同样的问题。