AVPlayer problem with HLS lives material and currentTime

410 Views Asked by At

I use AvPlayer to run HLS playlist in my iOS app. Everything works fine (seeking, play, pause and so on...) except 'currentTime' for LIVE materials.

Short Code:

public final class CustomPlayer: AVPlayer {
    static let periodicTimeObserverPeriod = 0.5
    
    var timeObserverToken: Any?

    //Other funcions and params
    
    override public func replaceCurrentItem(with item: AVPlayerItem?) {
        super.replaceCurrentItem(with: item)
        removePeriodicTimeObserver()
        if let item = item {
            addPeriodicTimeObserver()
        }
    }
    
    private func addPeriodicTimeObserver() {
        let time = CMTime(seconds: CustomPlayer.periodicTimeObserverPeriod, preferredTimescale: CMTimeScale(NSEC_PER_SEC))
        timeObserverToken = self.addPeriodicTimeObserver(forInterval: time, queue: .main, using: { [weak self] currentTime in
            guard let self = self else {
                return
            }
            print("NEW TIME in periodicTimeObserver: \(CMTimeGetSeconds(currentTime))")
            // Update position and other funcionalities
        })
    }
    
    private func removePeriodicTimeObserver() {
         // periodic time observer is removed
    }

}

Problem:

  1. After replaceCurrentItem in AvPlayer is called addPeriodicTimeObserver is run (where we update current position based on currentTime). Everything is made exactly the same way as in AvPlayer documentation.
  2. For movies everythings works fine (and above prints proper values in line print("NEW TIME in periodicTimeObserver: \(CMTimeGetSeconds(currentTime))"))
  3. For lives there are some issues: 0. Assume that: we start watching LIVE material X, for this material we have lets say T = 60 min of time shifting (so we can go back 60 min from the current date)
    1. We start watching material X from current date position (live)
    2. For above print we receive some strange small values:
      • NEW TIME in periodicTimeObserver: 20.13
      • NEW TIME in periodicTimeObserver: 20.72
      • ....
    3. Then we go back ~30 min and it works fine, but in periodicTimeObserver we have:
      • NEW TIME in periodicTimeObserver: 1805.56
      • NEW TIME in periodicTimeObserver: 1806.21
      • ...
    4. Then we came back to watching LIVE (seek to current date) and in periodicTimeObserver we have:
      • NEW TIME in periodicTimeObserver: 3597.86
      • NEW TIME in periodicTimeObserver: 3598.31
      • ...
    5. After that, the behaviour is always be the same as in steps 3. and 4. As far as I understand it is correct, according to the assumptions we can go back 60 min, i.e. 3600 sec, so the positions can be defined more: [0, ..... <= 3599] where ~3599 is current date(LIVE)

QUESTION: Do you have any idea whats wrong in 2. and why at the beggining i receive such a strange values? It's very strange case and i even don't know how to debug it. I thought that maybe HLS playlist are not defined properly, but they looks ok. Any suggestions what should be checked and where should i dig?

1

There are 1 best solutions below

0
Warren Burton On

Seems reasonable.

AVPlayer is a dynamic object whose state continuously changes. There are two approaches you can use to observe a player’s state:

Until the point you wind back the play head currentTime is reporting the delta from your start.

You then wind back and currentTime reports the delta from a potential 60 minutes back from now.

I’d be looking at the values for reversePlaybackEndTime and reversePlaybackStartTime on the AVPlayerItem to see if they can provide more information