【问题标题】:Why does AVAudioPlayer keep playing when headphones are unplugged?为什么拔掉耳机后 AVAudioPlayer 一直在播放?
【发布时间】:2018-02-22 11:32:00
【问题描述】:

苹果表示,当耳机拔出时,iOS会自动停止播放。

虽然在使用 AVPlayer 时确实如此,但在使用 AVAudioPlayer 时实际上并没有按预期工作,而是继续从内置扬声器播放音频。 p>

苹果网站链接: https://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/HandlingAudioHardwareRouteChanges/HandlingAudioHardwareRouteChanges.html

【问题讨论】:

    标签: ios swift avplayer avaudioplayer


    【解决方案1】:

    我不确定,但试试这个:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        NSError *setCategoryErr;
        [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
    
        // Detects when the audio route changes (ex: headphones unplugged)
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioHardwareRouteChanged:) name:AVAudioSessionRouteChangeNotification object:nil];
        // Don't forget to remove notification in dealloc method!!
    }
    
    - (void)audioHardwareRouteChanged:(NSNotification *)notification {
        NSInteger routeChangeReason = [notification.userInfo[AVAudioSessionRouteChangeReasonKey] integerValue];
        if (routeChangeReason == AVAudioSessionRouteChangeReasonOldDeviceUnavailable) {
            // if we're here, The old device is unavailable == headphones have been unplugged, so stop manually!
            [self.player stop];
        }
    }
    

    【讨论】:

      【解决方案2】:

      你需要观察硬件路由变化观察器并基于回调,你可以停止播放。

      设置您的播放器 - 播放音频(即使在静音模式下)并使其他音乐静音:

      let audioSession = AVAudioSession.sharedInstance()
      _ = try? audioSession.setCategory(AVAudioSessionCategoryPlayback, with: .duckOthers)
      _ = try? audioSession.setActive(true)
      NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(audioRouteChanged), name: .AVAudioSessionRouteChange, object: nil)
      
      
      func audioRouteChanged(note: Notification) {
         if let userInfo = note.userInfo {
             if let reason = userInfo[AVAudioSessionRouteChangeReasonKey] as? Int  {
             if reason == AVAudioSessionRouteChangeReason.oldDeviceUnavailable.hashValue {
             // headphones plugged out
             player.stop()
            }
          }
        }
      }
      

      重要提示:如果路由更改原因是 AVAudioSessionRouteChangeReasonOldDeviceUnavailable,媒体播放应用应该暂停播放,但如果原因是 AVAudioSessionRouteChangeReasonOverride,则不应暂停播放。

      【讨论】:

        【解决方案3】:

        来自document you linked to

        重要提示:如果路由更改原因是AVAudioSessionRouteChangeReasonOldDeviceUnavailable,媒体播放应用应该暂停播放,但如果原因是AVAudioSessionRouteChangeReasonOverride,则不应。

        “应该”可以表示职责或事件发生的可能性。 在这种情况下,这意味着有责任在耳机等被拔出时实施暂停,并且不会在您没有任何干预的情况下可能会发生这种情况(因为您'见过,它没有)。

        以新鲜的眼光看待它,文档模棱两可。应该不是一个好的词选择。

        【讨论】:

        • 这是一个好点,我又读了一遍引用的句子,它确实具有误导性。虽然我认为它确实回答了我的问题,但它仍然没有解释为什么 AVPlayer 会自动停止而 AVAudioPlayer 不会。我猜苹果只是前后矛盾。
        • 呵呵,应该是指概率吧!在任何情况下,AVPlayer 都被记录为在设备不可用时暂停,而 AVAudioPlayer 不是:developer.apple.com/documentation/avfoundation/avaudiosession/… 你可以记录一个针对 AVAudioPlayer 的错误,但它是一个非常古老且被遗弃的感觉类。我认为人们只使用它来监控它的液位。或者您可以观察通知并根据需要暂停您的 AVAudioPlayer。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-14
        相关资源
        最近更新 更多