Search code examples
iosswiftavfoundationavplayeravplayeritem

Detecting video stalls in AVPlayer


I am playing a HLS stream using AVPlayer API in my app. I would like to be able to detect situations where the video playback is stalled as result of insufficient network bandwidth or low connection quality.

Is there any reliable way of detecting if the video playback is actually playing normally on the screen?

During my testing I've seen situations where the following checks are all reporting OK statuses despite video playback being actually frozen on the screen:

AVPlayer.status = .readyToPlay
AVPlayer.timeControlStatus = .playing
AVPlayerItem.status = .readyToPlay
AVPlayerItem.isPlaybackBufferEmpty = false
AVPlayerItem.isPlaybackLikelyToKeepUp  = true

+ and also addBoundaryTimeObserver callbacks were fired.

Some of these eventually become negative, but only after a delay.

Is there is any 100% reliable way of detecting if a video is actually playing fine on the device?


Solution

  • To be sure a media is playing fine, check the AVPlayerItem's rate

    Also you can use AVPlayerItem's notifications to detect something's wrong:

    • AVPlayerItemPlaybackStalled
    • AVPlayerItemFailedToPlayToEndTime
    • AVPlayerItemDidPlayToEndTime

    for your information, AVPlayerItemDidPlayToEndTime can be triggered when buffer is empty AND network is out.

    you have an example here: PlaybackObservingService