2014-03-14 140 views
11

我的应用正在播放背景。暂停也很好,即使我的应用程序是背景使用注册为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]; 

     } 
    } 
} 
+0

你能弄明白吗?我有同样的问题。 – user1732055

+0

仍然存在相同的问题。 PlayAndRecord和Ambient会话类别不会收到“中断结束”通知。 SoloAmbient根本没有收到任何通知。 – kakyo

+0

你有没有调用-removeObserver:某处?我有类似的问题,这是原因。如果你和我一样,你可能想在“removeObserver”上做一个grep/find,以防你的眼睛看不到它...... – prewett

回答

0

还有就是你的AVCaptureSession实例的属性: @property(非原子)BOOL usesApplicationAudioSession

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

0

尝试添加对象:会话

[[NSNotificationCenter defaultCenter]的addObserver:自 选择器:@selector(onAudioSessionEvent :) 名:AVAudioSessionInterruptionNotification对象:[AVAudioSession sharedInstance]];