ios - AVPlayer 使用平移手势快进/后退

标签 ios avplayer uipangesturerecognizer avplayerlayer

在过去的两天里,我一直在用一个恼人的小故障敲我的头,希望有人能解释一下。
基本设置:我有一个带有播放器的 AVPlayerLayer, View 中有一个平移手势识别器,我希望用户能够来回滑动手指,视频会相应地搜索。
收获:我想如果用户抬起手指并再次将其放置在屏幕上的任何位置,以便在 处恢复快进或快退。准确 它停止的同一帧并从那里继续前进。
我看过这两个问题:
Pan Gesture with AVPlayer
Pan to seek AVPlayer
我在这里尝试过 Apple 的建议:https://developer.apple.com/library/content/qa/qa1820/_index.html
同样,但问题是每当我开始一个新的平移手势时,播放器都会跳几帧然后继续。
我的最新方法是在查找完成 block 完成后设置当前时间,然后我尝试将新的查找时间附加到该时间。
这是我的设置:

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;
}
任何帮助将不胜感激!

最佳答案

UIGestureRecognizerStateBegan保存 CMTime通过 AVPlayer然后进行增量更改平移,然后在 UIGestureRecognizerStateEnded只是寻求回到原来节省的时间?

只是为了更顺畅地寻找注意不要暂停视频,将速率设置为 0。

关于ios - AVPlayer 使用平移手势快进/后退,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41853654/

相关文章:

ios - 传递给不同 Controller 时 AVPlayer 持续时间会发生变化

ios - 将数组保存到文本文件中并快速附加到电子邮件

swift - 添加图层后,播放按钮不会出现在 AVPlayer 中 - iOS 16

ios - AVPlayer 覆盖视频控件

ios - 如何通过平移手势限制可移动 View

ios - 如何通过拖动 ImageView 的右角来调整 ImageView 的大小和旋转 ImageView

ios - 在 iOS 上使用智能卡进行 TLS 身份验证

iphone - Phonegap:适用于iOS4的应用

iphone - 检查用户是否在位置边界

ios - UIPanGesture 识别器仅向一个方向滑动