-
-
Save shaps80/ac16b906938ad256e1f47b52b4809512 to your computer and use it in GitHub Desktop.
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() | |
} | |
} | |
} |
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 👍
I invite you to have a look at my related comment on Stack Overflow for how smooth scrubbing can be achieved, providing content can support stepping.
Our implementation, which can be seen and tested in our player library, is currently at least on par with AVPlayerViewController
implementation (sometimes achieveing better results), whether the content supports stepping (frame-by-frame), I-frames or neither.
Thanks for sharing. I’ll have to give this a shot. I read your post and it sounds similar. One thing I’m curious about is not cancelling pending. This was actually recommended by someone on the AV team but I think your approach is different in that you use very different tolerances. But I’ll give it a shot because as you mentioned, one of the issues generally for getting fast almost frame-by-frame seeking depends on how it was encoded. But perhaps your solution has better more consistent results. If I get around to testing ill let you know either way 👍 Thanks again! Shaps
Side note btw regarding AVPlayerController. I believe at one point I discovered that actually they use the thumbnail generator to improve the perceived responsiveness. I did this in another product (simple video editor) alongside the code above and it had a drastic impact on the UX. but if your approach improves the actual seeking as well then that just another huge plus.
This was actually recommended by someone on the AV team but I think your approach is different in that you use very different tolerances.
I guess this could be recommended if you want to avoid downloading too many segments while seeking, but this of course depends on the desired user experience. As far as I can see AVPlayerViewController
nowadays seem to avoid seek cancellation, probably as described in Apple dedicated Q&A article.
Side note btw regarding AVPlayerController. I believe at one point I discovered that actually they use the thumbnail generator to improve the perceived responsiveness.
Note sure if they use AVAssetImageGenerator
directly but if HLS streams contain I-frames the player surely uses them (even without changing the actual player speed to values > 2, which I initially thought might be required).
BTW you can easily test the behavior our implementation achieves by installing our demo app from TestFlight. The first demo tab has a lot of different streams from several sources (with various encodings and packagings), including the standard Apple test streams. You can also use the demo to compare the behavior we obtain vs. the one provided by the system player UI since we also have included the vanilla system experience in our demo as well (2nd tab).
Oh that’s amazing thanks! Unfortunately your beta isn’t accepting lol but I trust you.
Should be fixed now, sorry, there was no recent build enabled for external testing 😇
👍👍
https://stackoverflow.com/a/17331242/2742007
avPlayer.currentItem?.step(byCount: isForward ? 1 : -1)
https://developer.apple.com/documentation/avfoundation/avplayeritem/1387968-step