Created
October 17, 2019 06:46
-
-
Save trilliwon/90f28b27de3dc7d1c8eb75714cf5470c to your computer and use it in GitHub Desktop.
AVPlayer Seek Smoothly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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