我试图在AVPlayer中平移和向前和向后寻找。这是一种工作,但是确定平移锅在何处平移到 Assets 长度的基本数学方法是错误的。有人可以提供协助吗?

- (void) handlePanGesture:(UIPanGestureRecognizer*)pan{

    CGPoint translate = [pan translationInView:self.view];
    CGFloat xCoord = translate.x;
    double diff = (xCoord);
    //NSLog(@"%F",diff);

    CMTime duration = self.avPlayer.currentItem.asset.duration;
    float seconds = CMTimeGetSeconds(duration);
    NSLog(@"duration: %.2f", seconds);

    CGFloat gh = 0;

    if (diff>=0) {
        //If the difference is positive
        NSLog(@"%f",diff);
        gh = diff;
    } else {
        //If the difference is negative
        NSLog(@"%f",diff*-1);
        gh = diff*-1;
    }

    float minValue = 0;
    float maxValue = 1024;
    float value = gh;

    double time = seconds * (value - minValue) / (maxValue - minValue);

    [_avPlayer seekToTime:CMTimeMakeWithSeconds(time, 10) toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
    //[_avPlayer seekToTime:CMTimeMakeWithSeconds(seconds*(Float64)diff , 1024) toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];

}

最佳答案

您没有标准化触摸位置和相应的时间值。两者之间是否存在1:1的关系?那是不可能的。

获取平移手势的最小和最大触摸位置值以及 Assets 持续时间的最小和最大值(显然,从零到视频的长度),然后应用以下公式将触摸位置转换为寻道时间:

// Map
#define map(x, in_min, in_max, out_min, out_max) ((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)

这是我编写的使用该公式的代码:
- (IBAction)handlePanGesture:(UIPanGestureRecognizer *)sender {
        if (sender.state == UIGestureRecognizerStateChanged){
            CGPoint location = [sender locationInView:self];
            float nlx = ((location.x / ((CGRectGetMidX(self.frame) / (self.frame.size.width / 2.0)))) / (self.frame.size.width / 2.0)) - 1.0;
            //float nly = ((location.y / ((CGRectGetMidY(self.view.frame) / (self.view.frame.size.width / 2.0)))) / (self.view.frame.size.width / 2.0)) - 1.0;
            nlx = nlx * 2.0;
            [self.delegate setRate:nlx];
        }
}

我选中了显示速率的标签以及在擦洗时出现的播放图标,并根据平移视频的快慢来改变大小。尽管您并没有要求,但只要提出要求即可。

哦,“两倍”因子旨在向发送给委托(delegate)人的setRate方法的平移手势值添加一条加速曲线。您可以为此使用任何公式,甚至可以使用实际曲线,例如pow(nlx,2.0)或其他任何形式。

关于ios - 潘寻求AVPlayer,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26046946/

10-13 08:20