Skip to content

Instantly share code, notes, and snippets.

@trilliwon
Created October 17, 2019 06:46
Show Gist options
  • Save trilliwon/90f28b27de3dc7d1c8eb75714cf5470c to your computer and use it in GitHub Desktop.
Save trilliwon/90f28b27de3dc7d1c8eb75714cf5470c to your computer and use it in GitHub Desktop.
AVPlayer Seek Smoothly
var isSeekInProgress = false
var chasingTime = CMTime.zero
func seekActually(time: CMTime) {
isSeekInProgress = true
playerView?.player.seek(to: time, toleranceBefore: CMTime.zero, toleranceAfter: CMTime.zero) { success in
if time == self.chasingTime {
self.isSeekInProgress = false
} else {
self.trySeekToChaseTime()
}
}
}
func trySeekToChaseTime() {
guard let status = playerView.player.currentItem?.status, status != .readyToPlay else { return }
seekActually(time: chasingTime)
}
func seekSmoothly(to time: CMTime) {
if chasingTime != time {
chasingTime = time
if !isSeekInProgress {
trySeekToChaseTime()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment