I use AVPlayer to play an audio livestream via internet. I like to recover playback if it was paused for longer than 1 minute.
I call player.rate = 1.0
Use the timeControlStatus of the AVPlayer instance which "indicates whether playback is currently in progress, paused indefinitely, or suspended while waiting for appropriate network conditions."
let status = player.timeControlStatus
switch status {
case .paused:
print("timeControlStatus.paused")
case .playing:
print("timeControlStatus.playing")
case .waitingToPlayAtSpecifiedRate:
print("timeControlStatus.waitingToPlayAtSpecifiedRate")
if let reason = player.reasonForWaitingToPlay {
switch reason {
case .evaluatingBufferingRate:
print("reasonForWaitingToPlay.evaluatingBufferingRate")
case .toMinimizeStalls:
print("reasonForWaitingToPlay.toMinimizeStalls")
case .noItemToPlay:
print("reasonForWaitingToPlay.noItemToPlay")
default:
print("Unknown \(reason)")
}
}
}
I have the same issue but in my case, it occurs if I go into background while in the paused state. When I come back to forground .play() does not work. It gets stuck the waitingToPlayAtSpecifiedRate.evaluatingBufferingRate mode. At that point the AVPlayerItem.status instance is readToPlay.
At this time of writing whenever a startCommand is received I reset the AVplayer to be sure. But this seems clunky. Looking for a smoother solution.