2016-11-01 4 views
0

이 기능은 내 앱에서 매우 중요하며 이에 대한 도움이 정말 필요합니다. 기본적으로 비디오 플레이어에 UIPanGestureRecognizer을 추가하여 사용자가 제스처로 비디오를 빨리 감기/되감기 할 수있게하려고합니다.AVPlayer를 사용한 팬 제스처

Apple에는 Swift 3을 사용하는 샘플 코드가 있으며 비디오 플레이어 전체가 이미 만들어져 있습니다. 누락 된 유일한 것은 UIPanGestureRecognizer입니다. https://developer.apple.com/library/content/samplecode/AVFoundationSimplePlayer-iOS/Introduction/Intro.html#//apple_ref/doc/uid/TP40016103

viewWillAppear에서 내가 지금처럼 제스처를 추가 :

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) 
     } 
    } 
관련 문제