潘寻求AVPlayer [英] Pan to seek AVPlayer

查看:145
本文介绍了潘寻求AVPlayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在我的AVPlayer中平移并向前和向后搜索。它有点工作,但确定平移转换为资产长度的基本数学是错误的。任何人都可以提供协助吗?

I am trying to pan and seek forwards and backwards in my AVPlayer. It is kind of working but the basic math of determining where the pan is translated to the length of the asset is wrong. Can any one offer assistance?

- (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的关系吗?这是不可能的。

You are not normalizing the touch location and the corresponding time values. Is there a 1:1 relationship between the two? That's not possible.

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

Take the minimum and maximum touch location values of the pan gesture and the minimum and maximum values of the asset's duration (obviously, from zero to the length of the video), and then apply the following formula to translate the touch location to the seek time:

// 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)

以下是我编写的使用该公式的代码:

Here's the code I wrote that uses that formula:

- (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];
        }
}

我剔除了显示费率的标签,以及您正在清洗时出现的播放图标,并根据您平移视频的速度或速度来更改大小。虽然你没有要求,如果你想要它,只要问。

I culled the label that displays the rate, and the Play icon that appears while you're scrubbing, and which changes sizes depending on how fast or slow you are panning the video. Although you didn't ask for that, if you want it, just ask.

哦,时间 - 两因素是为了增加加速曲线pan手势值发送给委托的setRate方法。您可以使用任何公式,甚至是实际曲线,如pow(nlx,2.0)或其他...

Oh, the "times-two" factor is intended to add an acceleration curve to the pan gesture value sent to the delegate's setRate method. You can use any formula for that, even an actual curve, like pow(nlx, 2.0) or whatever...

这篇关于潘寻求AVPlayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆