Hi all,
We are using AVPlayer to play our audio only HLS streams and are seeing what looks like a threading issue with the currentTime() method on AVPlayer vs the times reported by using the periodicTimeObserver.
Here's the scenario:
1. A class registers as a periodicTimeObserver using 0.1 seconds as the desired time interval.
2. Lets say the current time (as reported to the periodic observer) reaches 8.8 seconds.
3. On a separate thread, we have the player seek to the currentTime() + 2 seconds.
4. Within the completion block of the call to seek, we read & report what the new currentTime() is
5. While this is happening, you still listen to the periodicTimeObserver callbacks.
6. You'll notice the time reported by the call to currentTime() within the seek completion block is actually AFTER the currentTime() as reported by the periodicTimeObserver.
Here it is in code:
let underlyingPlayer: AVQueuePlayer = ...
underlyingPlayer.seek(to: newPosition, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero, completionHandler: { [weak self] success in
let time = underlyingPlayer.currentTime
print("After seeking, the current time is \(time)")
completionHandler?(error, time)
})
// periodic time observer is set up as the following with an interval of 0.1 seconds
let underlyingTimeObservingQueue = DispatchQueue(label: "com.company.player.timeObserver.queue", qos: DispatchQoS.userInitiated)
let valueInterval = CMTimeMakeWithSeconds(interval, 1000)
return underlyingPlayer.addPeriodicTimeObserver(forInterval: valueInterval, queue: underlyingTimeObservingQueue) { time in
print("periodic time is \(CMTimeGetSeconds(time))")
return block(CMTimeGetSeconds(time))
}
}
When you run this, here's the output we receive:
periodicTime is 7.701181537
periodicTime is 7.801130009
periodicTime is 7.900316636
periodicTime is 8.000758814
periodicTime is 8.100601498
periodicTime is 8.201145766
periodicTime is 8.301150409
periodicTime is 8.401138697
periodicTime is 8.501144148
periodicTime is 8.601161655
periodicTime is 8.701154072
periodicTime is 8.800160082
After seeking, the current time is 10.88 <----------------- This is the result returned by the seek call's completion block
periodicTime is 10.736309528
periodicTime is 10.736393196
periodicTime is 10.801075743
periodicTime is 10.900429325
periodicTime is 11.000907073
periodicTime is 11.100982256
periodicTime is 11.201173809
periodicTime is 11.300147113
periodicTime is 11.401182831
periodicTime is 11.500769324
periodicTime is 11.600593684
periodicTime is 11.701193966
periodicTime is 11.800258566
periodicTime is 11.900176181
periodicTime is 12.000423167
This seems like a bug within AVPlayer, but wanted to check here before filing it.
We couldn't really find any workarounds for this, but here's what we tried so far:
1. Unregistering all periodicTimeObservers right before seeking, and adding them back in the seek completion handler
2. Dispatch_sync the call to currentTime() within the seek method's completion block
However, none of these solutions fixed the problem.
Does anyone have any ideas about what's going on?