在后台播放音乐:AVAudioSessionInterruptionNotification未被触发 [英] Playing music at back ground: AVAudioSessionInterruptionNotification not fired
问题描述
我的应用正在背景中播放。 暂停和播放也可以正常运行,即使我的应用是后台使用<$注册的方法c $ c> AVAudioSessionInterruptionNotification 。
My app is playing in background well. Pause and play also works well even my app is background using the methods registered for AVAudioSessionInterruptionNotification
.
问题到达方案的步骤:
- 启动我的应用程序 - >从我的应用程序中播放背景音乐
- 启动Apple的音乐应用程序并播放。 AVAudioSessionInterruptionNotification已解雇。
- 当我暂停音乐应用或杀死音乐应用时。 AVAudioSessionInterruptionNotification不会被解雇
- Launch my app -> goes background music from my app playing well
- launch apple's music app and play. AVAudioSessionInterruptionNotification fired.
- When i pause the music app or kill the music app. AVAudioSessionInterruptionNotification doesn't get fired
代码:
我在中这样做的appdelegate 即可。 didfinishlaunching
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];
}
}
}
推荐答案
尝试添加对象:session
Try to add object:session
[[NSNotificationCenter defaultCenter] addObserver:self
selector: @selector(onAudioSessionEvent :)
name:AVAudioSessionInterruptionNotification object:[AVAudioSession
sharedInstance]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAudioSessionEvent:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];
这篇关于在后台播放音乐:AVAudioSessionInterruptionNotification未被触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!