我正在尝试在 AVPlayer 中向前和向后平移和搜索。这是可行的,但确定平移在何处转换为资产长度的基本数学是错误的。有人可以提供帮助吗?
- (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的关系吗?那是不可能的。
获取平移手势的最小和最大触摸位置值以及资源持续时间(显然,从零到视频长度)的最小和最大值,然后应用以下公式将触摸位置转换为搜索时间:
// 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];
}
}
我挑选了显示速率的标签,以及在您拖动时出现的播放图标,该图标会根据您平移视频的快慢来改变大小。虽然你没有要求,但如果你想要,就问吧。
哦,“两倍”因子旨在将加速度曲线添加到发送到委托的 setRate 方法的平移手势值。您可以使用任何公式,甚至是实际曲线,例如 pow(nlx, 2.0) 或其他......
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)