Skip to content

Instantly share code, notes, and snippets.

@shaps80
Last active April 18, 2024 22:31
Show Gist options
  • Star 45 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save shaps80/ac16b906938ad256e1f47b52b4809512 to your computer and use it in GitHub Desktop.
Save shaps80/ac16b906938ad256e1f47b52b4809512 to your computer and use it in GitHub Desktop.
Enables smooth frame-by-frame scrubbing (in both directions) – similar to Apple's applications.
public enum Direction {
case forward
case backward
}
internal var player: AVPlayer?
private var isSeekInProgress = false
private var chaseTime = kCMTimeZero
private var preferredFrameRate: Float = 23.98
public func seek(to time: CMTime) {
seekSmoothlyToTime(newChaseTime: time)
}
public func stepByFrame(in direction: Direction) {
let frameRate = preferredFrameRate
?? player?.currentItem?.tracks
.first(where: { $0.assetTrack.mediaType == .video })?
.currentVideoFrameRate
?? -1
let time = player?.currentItem?.currentTime() ?? kCMTimeZero
let seconds = Double(1) / Double(frameRate)
let timescale = Double(seconds) / Double(time.timescale) < 1 ? 600 : time.timescale
let oneFrame = CMTime(seconds: seconds, preferredTimescale: timescale)
let next = direction == .forward
? CMTimeAdd(time, oneFrame)
: CMTimeSubtract(time, oneFrame)
seekSmoothlyToTime(newChaseTime: next)
}
private func seekSmoothlyToTime(newChaseTime: CMTime) {
if CMTimeCompare(newChaseTime, chaseTime) != 0 {
chaseTime = newChaseTime
if !isSeekInProgress {
trySeekToChaseTime()
}
}
}
private func trySeekToChaseTime() {
guard player?.status == .readyToPlay else { return }
actuallySeekToTime()
}
private func actuallySeekToTime() {
isSeekInProgress = true
let seekTimeInProgress = chaseTime
player?.seek(to: seekTimeInProgress, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero) { [weak self] _ in
guard let `self` = self else { return }
if CMTimeCompare(seekTimeInProgress, self.chaseTime) == 0 {
self.isSeekInProgress = false
} else {
self.trySeekToChaseTime()
}
}
}
@beezital
Copy link

@shaps80
Copy link
Author

shaps80 commented Dec 14, 2022

https://stackoverflow.com/a/17331242/2742007

avPlayer.currentItem?.step(byCount: isForward ? 1 : -1)

https://developer.apple.com/documentation/avfoundation/avplayeritem/1387968-step

This is not relevant I'm afraid. Or more importantly it's incomplete. If you read (or even try) that API out yourself, you'll note that it's not performant in most user-driven cases and cannot chase smoothly. This is because it doesn't cancel previous seeks.

The provided example solves this problem for high performance cases. The API you're suggesting is fine for non-interactive scenarios though 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment