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

编程入门 行业动态 更新时间:2024-10-26 08:22:42
本文介绍了在后台播放音乐:AVAudioSessionInterruptionNotification未被触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的应用正在背景中播放。 暂停和播放也可以正常运行,即使我的应用是后台使用 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.

问题到达方案的步骤:

  • 启动我的应用程序 - >从我的应用程序中播放背景音乐
  • 启动Apple的音乐应用程序并播放。 AVAudioSessionInterruptionNotification已解雇。
  • 当我暂停音乐应用或杀死音乐应用时。 AVAudioSessionInterruptionNotification不会被解雇
  • Launch my app -> goes background music from my app playing well
  • launch apple's music app and play. AVAudioSessionInterruptionNotification fired.
  • When i pause the music app or kill the music app. AVAudioSessionInterruptionNotification doesn't get fired
  • 代码:我在中这样做的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未被触发

    本文发布于:2023-07-11 09:04:59,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1093514.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:未被   后台   音乐   AVAudioSessionInterruptionNotification

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!