使用AVPlayer进行Pan Gesture [英] Pan Gesture with AVPlayer

查看:274
本文介绍了使用AVPlayer进行Pan Gesture的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此功能对我的应用非常重要,并且非常需要一些帮助。基本上,我想向视频播放器添加 UIPanGestureRecognizer ,以便用户可以使用手势快进/快退视频。

This feature is really important to my app and would really like some help on it. Basically, I want to add a UIPanGestureRecognizer to a video player so that the user can fast forward/rewind the video with the gesture.

Apple有一些使用Swift 3的示例代码,并且已经创建了整个视频播放器。唯一缺少的是 UIPanGestureRecognizer 。这是链接: https://developer.apple.com/library/content/samplecode/AVFoundationSimplePlayer-iOS/Introduction/Intro.html#//apple_ref/doc/uid/TP40016103

Apple has some sample code which uses Swift 3 and has the entire video player already created. 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

viewWillAppear 我添加了这样的手势:

In viewWillAppear I added the gesture like so:

let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture))
view.addGestureRecognizer(panGesture)

这是有点工作的代码。它目前正在擦洗。

This is the code that is working somewhat. It is currently scrubbing.

问题是每次我开始平移手势时,视频会跳到中间,然后从那里开始。平移手势不是从视频当前位置快进/快退,而是将视频跳到中间,然后允许我快进/快退,这是不好的。

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!

推荐答案

我用 AVPlayer 实现了 UIPanGesture ,下面是我的工作代码 [swift 3]

i've implemented UIPanGesture with AVPlayer, below is my working code [swift 3].

func handleDraging(_ gesture:UIPanGestureRecognizer){
        let location = gesture.location(in: self.playerView)
        if gesture.state == .began {
            print("\n\n-->---> Panning.state = .began at Point.x = \(location.x)")
            self.playerLayer.player?.rate = 0
            self.stopTimer()
        }else if gesture.state == .changed {
            let valocity = gesture.velocity(in: self.playerView)
            print("\n\n-->---> Panning.state = .changed at Point.x = \(location.x)")
            let percentage = Double(location.x) / Double(self.playerView.frame.width)
            let currentTime = self.playerLayer.player!.currentTime()
            let currentSeconds = CMTimeGetSeconds(currentTime)
            var newSeconds = currentSeconds + percentage
            if valocity.x < 0 {
                newSeconds = currentSeconds - percentage
            }
            let newTime = CMTime(seconds: newSeconds, preferredTimescale: self.playerLayer.player!.currentTime().timescale)
            self.playerLayer.player?.seek(to: newTime, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero)
            let total = CGFloat(CMTimeGetSeconds(self.playerLayer.player!.currentItem!.asset.duration))
            let seconds = CGFloat(CMTimeGetSeconds(newTime))
            self.interval = Double(seconds)//here update your CurrentTimelabel.text .....

            let temp = seconds/total
            self.progress = Float(temp)//here update your UIProgressBar.progress .....
            print("\n\t-->Total Progress = \(temp)")
        }else if gesture.state == .ended || gesture.state == .failed || gesture.state == .recognized {
            gesture.setTranslation(CGPoint.zero, in: self.playerView)
            self.startTimer()
            self.playerLayer.player?.playImmediately(atRate: 1)
        }
    }

这篇关于使用AVPlayer进行Pan Gesture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆