AVPlayer audioSessionGotInterrupted notification when waking up from background

后端 未结 2 904
Happy的楠姐
Happy的楠姐 2021-02-06 03:11

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

I implemented the audioSessionGotInterrupted

2条回答
  •  滥情空心
    2021-02-06 03:53

    Check this

    https://developer.apple.com/documentation/avfoundation/avaudiosession/1616596-interruptionnotification

    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.

    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
      }
    }
    

提交回复
热议问题