从后台唤醒时,AVPlayer audioSessionGotInterrupted通知 [英] AVPlayer audioSessionGotInterrupted notification when waking up from background

查看:149
本文介绍了从后台唤醒时,AVPlayer audioSessionGotInterrupted通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AVAudioPlayer播放音频.我启用了背景音频,并且音频会话配置正确.

I use AVAudioPlayer to play audio. I have background audio enabled and the audio sessions are configured correctly.

我实现了audioSessionGotInterrupted方法,以通知音频会话是否中断.这是我当前的代码:

I implemented the audioSessionGotInterrupted method to be informed if the audio session gets interrupted. This is my current code:

@objc private func audioSessionGotInterrupted(note: NSNotification) {

    guard let userInfo = note.userInfo,
        let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
        let type = AVAudioSessionInterruptionType(rawValue: typeValue) else {
            return
    }

    if type == .began {
        print("interrupted")
        // Interruption began, take appropriate actions
        player.pause()
        saveCurrentPlayerPosition()
    }
    else if type == .ended {
        if let optionsValue = userInfo[AVAudioSessionInterruptionOptionKey] as? UInt {
            let options = AVAudioSessionInterruptionOptions(rawValue: optionsValue)
            if options == .shouldResume {
                print("restored")
                // Interruption Ended - playback should resume
                setupPlayer()
                player.play()
            } else {
                // Interruption Ended - playback should NOT resume
                // just keep the player paused
            }
        }
    }
}

现在,我要执行以下操作:

Now I do the following:

  • 播放一些音频
  • 锁定手机
  • 暂停音频
  • 等待几秒钟,直到在XCode调试器中看到该应用已在后台停止
  • 我在锁定屏幕上点击了播放

我的commandCenter play()方法被按预期方式调用.但是,audioSessionGotInterrupted方法也被type == .began调用.

My commandCenter play() methods gets called as expected. However also the audioSessionGotInterrupted method gets called with type == .began.

那怎么可能?我希望看不到任何此类通知,至少不会看到.ended

How is that possible? I expect to see no notification of that kind or at least .ended

我使用iOS 10 beta 8.

I use iOS 10 beta 8.

推荐答案

选中此

https://developer.apple.com/documentation/avfoundation/avaudiosession/1616596-中断通知

从iOS 10开始,系统将响应于应用程序进程被挂起而停用大多数应用程序的音频会话.当应用再次开始运行时,它将收到一个中断通知,说明其音频会话已被系统停用.此通知必须延迟时间,因为只有在应用再次运行时才能传递该通知.如果您的应用程序的音频会话由于这个原因而被暂停,则userInfo词典将包含值为true的AVAudioSessionInterruptionWasSuspendedKey键.

Starting in iOS 10, the system will deactivate the audio session of most apps in response to the app process being suspended. When the app starts running again, it will receive an interruption notification that its audio session has been deactivated by the system. This notification is necessarily delayed in time because it can only be delivered once the app is running again. If your app's audio session was suspended for this reason, the userInfo dictionary will contain the AVAudioSessionInterruptionWasSuspendedKey key with a value of true.

如果您的音频会话被配置为不可混合(播放,playAndRecord,soloAmbient和multiRoute类别的默认行为),建议您在去时不积极使用音频的情况下停用音频会话进入后台.这样做可以避免您的音频会话被系统停用(并收到此令人困惑的通知).

If your audio session is configured to be non-mixable (the default behavior for the playback, playAndRecord, soloAmbient, and multiRoute categories), it's recommended that you deactivate your audio session if you're not actively using audio when you go into the background. Doing so will avoid having your audio session deactivated by the system (and receiving this somewhat confusing notification).

if let reason = AVAudioSessionInterruptionType(rawValue: reasonType as! UInt) {
  switch reason {
  case .began:
    var shouldPause = true
    if #available(iOS 10.3, *) {
      if let _ = notification.userInfo?[AVAudioSessionInterruptionWasSuspendedKey] {
        shouldPause = false
      }
    }
    if shouldPause {
      self.pause()
    }
    break
  case .ended:
    break
  }
}

这篇关于从后台唤醒时,AVPlayer audioSessionGotInterrupted通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆