Pan Gesture with AVPlayer

前端 未结 2 1650
南旧
南旧 2020-12-22 04:46

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 th

相关标签:
2条回答
  • 2020-12-22 05:03

    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)
            }
        }
    
    0 讨论(0)
  • 2020-12-22 05:14

    Thanks for your code. I used it, changed few lines, it's working well.

       @objc func handlePanGesture(sender: UIPanGestureRecognizer) {
          switch sender.state {
           case .began, .changed:
            let translation = sender.translation(in: self.view)
            let horizontalTranslation = Float(translation.x)
    
            let durationInSeconds = Float(CMTimeGetSeconds(player.currentItem!.asset.duration))
    
            // normalize based on duration and width of the view
            let scale:Float = durationInSeconds / Float(self.view.frame.width) / 4
            // then you need to add the current time of player to avoid it starting from the middle.            
            let seekTime = horizontalTranslation * scale + Float((self.player.currentItem?.currentTime().seconds)!)
    
            let cmTime = CMTimeMakeWithSeconds(Float64(Float(seekTime)), preferredTimescale: self.player.currentTime().timescale)
            player.seek(to: cmTime)
            print("horizontal translation \(horizontalTranslation) \n timeToSeekTo: \(seekTime)")
    
        default:
            print("default")
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题