0

I have an AVQueuePlayer which is working as expected except for after returning from a suspended state.

It's playing a HLS audio stream. The logic for my pause button is very simple

isPlaying ? player?.play() : player?.pause()

I've checked the state of the player and the currentItem, all of which say it's "playing". But the audio does not progress or playback

player.rate = 1
player.currentItem.asset.isPlayable = true
player.status == readyToPlay
player.currentItem.status == readyToPlay

I'm also setting the session with:

do {
    try session.setCategory(AVAudioSession.Category.playback, options: [])
    try session.setActive(true, options: [])
} catch {
    print("Failed to set session active")
}

AVPlayer won't resume after this

Any advice on how to troubleshoot this would be greatly appreciated. Do I need to keep track of the app entering a suspended state and reload the AVPlayerItem?

User4
  • 1,330
  • 2
  • 15
  • 18

1 Answers1

0

Do I need to keep track of the app entering a suspended state and reload the AVPlayerItem?

It is not suspension of the app that you need to keep track of; it's interruption of the audio session. AVAudioSession provides a notification for exactly this purpose.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • See https://developer.apple.com/documentation/avfoundation/avaudiosession/responding_to_audio_session_interruptions – matt Oct 31 '20 at 17:55
  • Thanks @matt. I had tried the interruption handler and I do see it fires after coming back from suspension. I do have it working but it feels a bit hacky. I'm grabbing the current elapsed time, reinitialising the player and seeking to to the elapsed time. – User4 Nov 03 '20 at 19:36
  • I don't see why that's hacky. You were streaming and everything got hosed, so obviously you have to do _something_ to get going again. – matt Nov 03 '20 at 19:39