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:
- 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.
- For movies everythings works fine (and above prints proper values in line
print("NEW TIME in periodicTimeObserver: \(CMTimeGetSeconds(currentTime))")) - 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)
- We start watching material X from current date position (live)
- For above print we receive some strange small values:
- NEW TIME in periodicTimeObserver: 20.13
- NEW TIME in periodicTimeObserver: 20.72
- ....
- 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
- ...
- 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
- ...
- 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?
Seems reasonable.
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