This feature is really important for my application, and I really need help. Basically, I want to add a UIPanGestureRecognizer to a video player so that the user can speed up fast-forward / rewind the video with gestures.
Apple has sample code that uses Swift 3 and has an already created video player. The only thing missing is the UIPanGestureRecognizer . This is the link: https://developer.apple.com/library/content/samplecode/AVFoundationSimplePlayer-iOS/Introduction/Intro.html#//apple_ref/doc/uid/TP40016103
In viewWillAppear I added the same gesture:
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture)) view.addGestureRecognizer(panGesture)
This is code that works somewhat. Currently it is being cleaned.
The problem is that every time I start a panorama gesture, the video skips the middle and I start from there. Instead of fast-forwarding / rewinding from which the video is currently located, the panorama gesture skips the video to the middle, and then allows you to fast forward / rewind, which is not very good.
func handlePanGesture(sender: UIPanGestureRecognizer) { switch sender.state { case .began, .changed: let translation = sender.translation(in: self.view) var horizontalTranslation = Float(translation.x) let durationInSeconds = Float(CMTimeGetSeconds(player.currentItem!.asset.duration)) // Using 275 as the limit for delta along x let translationLimit: Float = 275 let minTranslation: Float = -1 * translationLimit let maxTranslation: Float = translationLimit if horizontalTranslation > maxTranslation { horizontalTranslation = maxTranslation } if horizontalTranslation < minTranslation { horizontalTranslation = minTranslation } let timeToSeekTo = normalize(delta: horizontalTranslation , minDelta: minTranslation, maxDelta: maxTranslation, minDuration: 0, maxDuration: durationInSeconds) print("horizontal translation \(horizontalTranslation) \n timeToSeekTo: \(timeToSeekTo)") let cmTime = CMTimeMakeWithSeconds(Float64(timeToSeekTo), self.player.currentTime().timescale) player.seek(to: cmTime) default: print("default") } } func normalize(delta: Float, minDelta: Float, maxDelta: Float, minDuration: Float, maxDuration: Float) -> Float { let result = ((delta - minDelta) * (maxDuration - minDuration) / (maxDelta - minDelta) + minDuration) return result }
Any help would be great - thanks!
source share