2016-11-01 23 views
0

この機能は私のアプリにとって本当に重要なことであり、実際にはこの機能が役立つと思います。基本的には、ビデオプレーヤーにUIPanGestureRecognizerを追加して、ユーザーがジェスチャーでビデオを早送り/巻き戻しできるようにしたいと思います。AVPlayerを使ったパンのジェスチャー

AppleにはSwift 3を使用するサンプルコードがあり、ビデオプレーヤー全体が既に作成されています。欠けているのはUIPanGestureRecognizerです。これは、リンクです:https://developer.apple.com/library/content/samplecode/AVFoundationSimplePlayer-iOS/Introduction/Intro.html#//apple_ref/doc/uid/TP40016103viewWillAppear

ので、同様に、私はジェスチャーを追加しました:

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

これは多少働いているコードです。それは現在スクラブしています。

問題は、私がパンジェスチャーを開始するたびに、ビデオが途中までスキップしてそこから開始することです。ビデオの現在の位置からの早送り/巻き戻しの代わりに、パンジェスチャーはビデオを途中までスキップして、早送り/巻き戻しを可能にします。

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 
} 

すべてのヘルプは素晴らしいことだ - ありがとう!

答えて

0

UIPanGestureAVPlayerで実装しました。以下は私の作業コード[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) 
     } 
    }