潘寻求AVPlayer

我正在试图在AVPlayer中向前和向后寻找。 这是一种有效的工作,但确定锅的翻译到资产的长度的基本math是错误的。 任何人可以提供帮助吗?

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

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

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

我剔除了显示比率的标签,以及在擦洗时出现的“播放”图标,以及根据您平移video的速度有多快而改变的大小。 虽然你没有要求,如果你想要的话,问问。

哦,“times-two”因子是为了向发送到委托的setRate方法的平移手势值添加加速曲线。 你可以使用任何公式,甚至一个实际的曲线,像pow(nlx,2.0)或其他…

如果你想使它更精确和有用,你应该实现不同的“灵敏度水平”。

苹果公司用他们的滑块做到这一点:如果你从滑块拖到两边,那么video移动的速度就会改变。 你越远离滑块越精确得到/越less,你可以达到。