在后台播放音乐:AVAudioSessionInterruptionNotification未被触发

jai*_*ani 11 ios avaudiosession

我的应用程序在后台 播放得很好.即使我的应用程序是使用注册的方法的后台,暂停播放也很有效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];

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Tee*_*hto 0

我在中断方面也遇到了类似的问题。iOS:Siri 不可用不会返回 AVAudioSessionInterruptionOptionShouldResume

最终使用 applicationWillResignActive 和 applicationDidBecomeActive 代替。