I'm using AVPlayer to play a video. I followed this video tutorial Let's Build That App
I use a UISlider/scrubber to go along with the current frame/time of the video.
I have a video that is 10.43 seconds and I use a fast forward function that takes me to the very end.
@objc fileprivate func fastForwardButtonTapped() {
guard let playerItem = playerItem else { return }
guard let player = player else { return }
let duration: Float64 = CMTimeGetSeconds(playerItem.duration)
let seekTime: CMTime = CMTimeMakeWithSeconds(duration, 1)
player.seek(to: seekTime)
}
The video goes to the very end but the problem is the slider only goes to the 10 sec point and I cannot get it to go to the last .43 secs. I get a warning message of:
The slider's value is determined in the player?.addPeriodicTimeObserver()
And because of this when I press fast forward instead of the slider going to the very end it stops a couple of points away (notice the white space):
How can I get more accurate values so that my slider can scrub all the way to the end?
playerItem?.addObserver(self, forKeyPath: #keyPath(AVPlayerItem.status),
options: [.old, .new],
context: &playerItemContext)
let interval = CMTime(value: 1, timescale: 2)
timeObserverToken = player?.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main, using: {
[weak self] (progressTime) in
let seconds = CMTimeGetSeconds(progressTime)
let secondsString = String(format: "%02d", Int(seconds) % 60)
let minutesString = String(format: "%02d", Int(seconds) / 60)
self?.currentTimeLabel.text = "\(minutesString):\(secondsString)"
if let duration = self?.playerItem!.duration{
let durationSeconds = CMTimeGetSeconds(duration)
self?.slider.value = Float(seconds / durationSeconds) // SLIDER IS UPDATED HERE
}
})
CMTimeScale(NSEC_PER_SEC)
instead of 1000. – Tomato