I am working on a SwiftUI app where users can apply effects at any arbitrary point in time to a video. After selecting an effect, it can be applied by starting and stopping the playback at desired times.
Besides the player, the UI also contains a scrubber, which shows thumbnails from the video and indicating the current timestamp.
The player and the scrubber are synced via a PeriodicTimeObserver, so the scrubber always moves along with the video. Since the player is working asynchronously, I am facing the problem that when the user pauses the playback, the observer is still being called 3 to 4 times afterward, which distorts the result. This is probably caused by some internal async behavior of the player.
Currently, I am "solving" this by using my own asynchronous pause implementation, which registers its own observer, and each time this gets called, a timer gets restarted. When this timer finishes, it calls a given callback.
It looks something like this (I know this is not actual swift code, I've simplified it a lot)
func pause(callback: () -> Void) {
let timer = Timer(interval: 3, repeat: false) {
callback()
}
timer.start()
self.addPeriodicTimeObserver(forInterval: 1, queue: .main) { _ in
timer.restart()
}
}
Does anyone have a better idea to know when the player actually stopped playing?