在背地播放音乐:AVAudioSessionInterruptionNotification未被触发

我的应用程序正在播放背景。 即使我的应用程序是使用为AVAudioSessionInterruptionNotification注册的方法的背景,也可以暂停播放

问题到达这个场景的步骤:

  1. 启动我的应用程序 – >从我的应用程序播放背景音乐
  2. 启动苹果的音乐应用程序和播放。 AVAudioSessionInterruptionNotification激发
  3. 当我暂停音乐应用程序或杀死音乐应用程序。 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:@"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]; } } } 

我有类似的问题与中断。 iOS:Siri不可用不会返回AVAudioSessionInterruptionOptionShouldResume

结束使用applicationWillResignActive和applicationDidBecomeActive来代替。

有你的AVCaptureSession实例的属性:@property(nonatomic)BOOL usesApplicationAudioSession

这是默认情况下,只是将其设置为NO,并会正常工作。

尝试添加对象:会话

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAudioSessionEvent 🙂 name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];