2017-01-25 4 views
0

지난 2 일 동안 내 머리를 괴롭 히고 누군가가 약간의 빛을 비출 수 있기를 바랬습니다.AVPlayer 팬 제스처로 빨리 감기/뒤로 건너 뛰기

기본 설정 : 플레이어에 볼 제스처 인식기가있는 AVPlayerLayer가 있으며 사용자가 손가락을 앞뒤로 스 와이프 할 수 있기를 원하며 그에 따라 비디오를 찾길 원합니다.

캐치 : 사용자가 어디서나 빠르게 앞으로 또는 뒤로의 화면에서 다시 자신의 손가락과 장소를 리프트 경우 내가 좋아하는 것이 중단 된 정확한 동일한 프레임에서 재개에서 진행 계속 그곳에. Pan Gesture with AVPlayer 내가 여기에 애플의 제안을 시도했습니다 Pan to seek AVPlayer

:

나는이 두 가지 질문 보았다뿐만 아니라 https://developer.apple.com/library/content/qa/qa1820/_index.html 을하지만, 문제는 내가 새로운 팬 제스처, 플레이어 점프를 시작할 때마다 몇 프레임 후 다시 시작합니다.

최근의 접근 방식은 검색 완료 블록이 완료되면 현재 시간을 설정 한 다음 새 검색 시간을 추가하려고했습니다.

self.item = [AVPlayerItem playerItemWithURL:resource]; 
self.player = [AVPlayer playerWithPlayerItem:self.item]; 
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player]; 

[self.view.layer addSublayer:self.playerLayer]; 
self.playerLayer.frame = self.view.bounds; 

UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)]; 

[self.view addGestureRecognizer:recognizer]; 

그리고 내 제스처 인식기 취급 :

- (void)swipe:(UIPanGestureRecognizer *)paramRecognizer 
{ 
    switch(paramRecognizer.state) { 
     // case UIGestureRecognizerStateBegan: 
     case UIGestureRecognizerStateChanged: 
     { 
      [self.player pause]; 

      CGPoint translation = [paramRecognizer translationInView:self.view]; 

      float horizontalTranslation = translation.x; 

      float durationInSeconds = CMTimeGetSeconds(self.player.currentItem.asset.duration); 

      //I'd like to be able to swipe across the whole view. 
      float translationLimit = self.view.bounds.size.width; 
      float minTranslation = 0; 
      float maxTranslation = translationLimit; 

      if (horizontalTranslation > maxTranslation) { 
       horizontalTranslation = maxTranslation; 
      } 

      if (horizontalTranslation < minTranslation) { 
       horizontalTranslation = minTranslation; 
      } 

     float timeToSeekTo = [self normalize:horizontalTranslation 
           andMinDelta:minTranslation 
           andMaxDelta:maxTranslation 
           andMinDuration:0 
           andMaxDuration:durationInSeconds]; 

      if(CMTIME_IS_VALID(self.currentTime)){ 
       float seconds = self.currentTime.value/self.currentTime.timescale; 

       [self.player seekToTime:CMTimeMakeWithSeconds(seconds+timeToSeekTo, self.player.currentTime.timescale) 
         toleranceBefore:kCMTimeZero 
         toleranceAfter:kCMTimeZero completionHandler:^(BOOL finished) 
       { 
        if(finished) 
        self.currentTime = self.player.currentItem.currentTime; 
       }]; 
      } 
      else 
      { 
       [self.player seekToTime:CMTimeMakeWithSeconds(timeToSeekTo, 
                   self.player.currentTime.timescale) toleranceBefore:kCMTimeZero 
         toleranceAfter:kCMTimeZero completionHandler:^(BOOL finished) { 
          if(finished) 
        self.currentTime = self.player.currentItem.currentTime; 
       }]; 
      } 

     } 
      break; 
    } 
} 

평준화 방법은 이것이다 : 여기

내 설정이다

- (float) normalize:(float)delta 
    andMinDelta:(float)minDelta 
    andMaxDelta:(float)maxDelta 
andMinDuration:(float)minDuration 
andMaxDuration:(float)maxDuration{ 

float result = ((delta - minDelta) * (maxDuration - minDuration)/(maxDelta - minDelta) + minDuration); 
return result; 

}

어떤 도움이 것 극단적이다 감사합니다!

+0

이 3.00 초 눌러 회전 시작하고 팬 2 픽셀 다음, 그것은에서 재생해야 들어 말한다면 약간 혼란 문구가 당신이 말하는 5.00 또는 3.00 다시? – SeanLintern88

+0

@ SeanLintern88It @ SeanLintern88It 3.0에서 다시 재생해야합니다. 미안해졌습니다. –

+0

확인 만하면됩니다. 3.0에서 패닝하고 뒤로/뒤로 이동 한 다음 .end로 돌아가서 3.0으로 되돌아갑니다. 그러나 스크럽 비디오 동안 팬을 기반으로 부드럽게 앞뒤로 갈 것인가? 그렇다면이 문제는 정확히 무엇입니까? – SeanLintern88

답변

0

UIGestureRecognizerStateBegan에서 CMTime부터 AVPlayer까지 변경 한 다음 델타 변경 패닝을 수행 한 다음 UIGestureRecognizerStateEnded에서 원래 저장된 시간으로 다시 검색하나요?

그냥 부드러운 추구에 대한 메모가 비디오를 일시 정지하지 않습니다에 속도를 설정 0

+0

당신이 정말로 옳습니다, 당신이 내 샘플 코드를 보면, 내가 UIGestureRecognizerStateBegan이 아닌 UIGestureRecognizerStateChanged에서 초기 탐색 시간을 설정 했으므로, 귀하의 코멘트를 읽은 후에 문제를 발견했습니다. 따라서 사용자가 시작할 때 초기 얼간이를보고있는 이유는 무엇입니까? 스 와이프. –

+0

또한 일시 중지하지 않고 일시 중지 비율을 사용하면 더 많은 유동성을 찾습니다. – SeanLintern88

관련 문제