riverhawk
riverhawk

Reputation: 236

Swift - UISlider as video scrubber smoothness issue

so I've been working on a video scrubber using the UISlider and for the most part it works great. It is able to smoothly move frame by frame through the video FORWARD, but when I move backwards it gets choppy and isn't as smooth. Below is my code for scrubbing the video:

func sliderBeganTracking(slider: CustomUISlider!) {
    if isPlaying == true {
        self.touchCount += 1
        pauseVideo()
    }
}

func sliderValueChanged(slider: CustomUISlider!) {
    let videoDuration = CMTimeGetSeconds(player.currentItem!.duration)
    let elapsedTime: Float64 = videoDuration * Float64(chosePosSlider.value)
    let time: CMTime = CMTimeMakeWithSeconds(elapsedTime, self.player.currentTime().timescale)

    self.player.seekToTime(time, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero)
}

Anyone know why it stops running smoothly when I try to scrub backwards and/or are there any better solutions or ways to go about creating a UISlider that scrubs the video smoothly? T

Upvotes: 2

Views: 1427

Answers (1)

Zack
Zack

Reputation: 1003

The issue at play (pun not intended) here has to do with the way videos are encoded for playback. Almost all video standards use a model of "keyframes", where the entire image for the single frame is encoded, and then a series of smaller delta changes that note which pixels to change from the last keyframe. This allows you to store a whole video without storing every pixel of every frame.

While this works great when the video is playing forward, because the OS can just apply the delta changes to the pixels it already has in memory, it doesn't work great when it's playing backwards, because it's not possible to "undo" the deltas in the same way. That's why you get smooth playback going forward, but when you go backwards things get choppy because the OS is jumping backwards to the nearest keyframe.

Your solutions are the same as the ones from this question about negative playback rates. The TLDR; is to re-encode the video in question into a format where every frame is a keyframe. You can do this in video software or by converting the video to a format like PhotoJPEG.

Upvotes: 1

Related Questions