【发布时间】:2014-03-14 09:02:52
【问题描述】:
我的应用在后台运行良好。暂停和play也运行良好,即使我的应用是使用为@987654321注册的方法的后台@。
问题到达场景的步骤:
- 启动我的应用程序 -> 从我的应用程序中播放背景音乐,播放效果很好
- 启动苹果音乐应用程序并播放。 AVAudioSessionInterruptionNotification 已触发。
- 当我暂停音乐应用程序或终止音乐应用程序时。 AVAudioSessionInterruptionNotification 不会被触发
代码: 我在 appdelegate 中执行此操作。 完成启动
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error: nil];
self.player = [[AVQueuePlayer alloc] initWithURL:[NSURL URLWithString:@"http://someurl.com:8040/;stream.mp3"]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAudioSessionEvent:) name:AVAudioSessionInterruptionNotification object:nil];
- (void) onAudioSessionEvent: (NSNotification *) notification
{
//Check the type of notification, especially if you are sending multiple AVAudioSession events here
NSLog(@"Interruption notification name %@", notification.name);
if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {
NSLog(@"Interruption notification received %@!", notification);
//Check to see if it was a Begin interruption
if ([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeBegan]]) {
NSLog(@"Interruption began!");
} else if([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeEnded]]){
NSLog(@"Interruption ended!");
//Resume your audio
NSLog(@"Player status %i", self.player.status);
// Resume playing the audio.
[self.player play];
}
}
}
【问题讨论】:
-
你能弄明白吗?我也有同样的问题。
-
仍然有同样的问题。 PlayAndRecord 和 Ambient 会话类别不会收到“中断结束”通知。 SoloAmbient 根本没有收到任何通知。
-
你有没有打电话给 -removeObserver: 某处?我有一个类似的问题,这就是原因。如果你像我一样,你可能想在“removeObserver”上做一个 grep/find,以防你的眼睛看不到它......
标签: ios avaudiosession