【问题标题】:Playing music at back ground: AVAudioSessionInterruptionNotification not fired在后台播放音乐:未触发 AVAudioSessionInterruptionNotification
【发布时间】:2014-03-14 09:02:52
【问题描述】:

我的应用在后台运行良好暂停play也运行良好,即使我的应用是使用为@987654321注册的方法的后台@。

问题到达场景的步骤:

  1. 启动我的应用程序 -> 从我的应用程序中播放背景音乐,播放效果很好
  2. 启动苹果音乐应用程序并播放。 AVAudioSessionInterruptionNotification 已触发
  3. 当我暂停音乐应用程序或终止音乐应用程序时。 AVAudioSessionInterruptionNotification 不会被触发

代码: 我在 appdelegate 中执行此操作。 完成启动

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];

        }
    }
}

【问题讨论】:

  • 你能弄明白吗?我也有同样的问题。
  • 仍然有同样的问题。 PlayAndRecord 和 Ambient 会话类别不会收到“中断结束”通知。 SoloAmbient 根本没有收到任何通知。
  • 你有没有打电话给 -removeObserver: 某处?我有一个类似的问题,这就是原因。如果你像我一样,你可能想在“removeObserver”上做一个 grep/find,以防你的眼睛看不到它......

标签: ios avaudiosession


【解决方案1】:

我遇到了类似的中断问题。 iOS: Siri not available does not return AVAudioSessionInterruptionOptionShouldResume

最终改用 applicationWillResignActive 和 applicationDidBecomeActive。

【讨论】:

    【解决方案2】:

    您的 AVCaptureSession 实例有属性: @property(nonatomic) BOOL 使用ApplicationAudioSession

    默认是YES,只要设置为NO,就可以正常工作了。

    【讨论】:

      【解决方案3】:

      尝试添加对象:会话

      [[NSNotificationCenter defaultCenter] addObserver:self 选择器:@selector(onAudioSessionEvent:) 名称:AVAudioSessionInterruptionNotification 对象:[AVAudioSession 共享实例]];

      【讨论】:

      • 如果它不为object:nil 触发,它不会为其他任何东西触发...
      猜你喜欢
      • 1970-01-01
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      • 2015-09-06
      • 1970-01-01
      • 2022-09-30
      • 2021-09-03
      • 1970-01-01
      相关资源
      最近更新 更多