我的应用正在背景中播放。 暂停和播放也可以正常运行,即使我的应用是后台使用 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.
问题到达方案的步骤:
代码:我在中这样做的appdelegate 即可。 didfinishlaunching
NSError *sessionError = nil; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError]; [[AVAudioSession sharedInstance] setActive:YES error: nil]; self.player = [[AVQueuePlayer alloc] initWithURL:[NSURL URLWithString:@"someurl: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未被触发
发布评论