[英]Pan to seek AVPlayer
我試圖在我的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];
}
}
我剔除了顯示速率的標簽,以及在您擦洗時出現的播放圖標,並根據您平移視頻的速度或速度來更改尺寸。 雖然你沒有要求,如果你想要它,只要問。
哦,“times-two”因子旨在為發送給委托的setRate方法的平移手勢值添加加速度曲線。 您可以使用任何公式,甚至是實際曲線,如pow(nlx,2.0)或其他......
如果你想讓它更精確和有用,你應該實現不同的“靈敏度”。
Apple使用滑塊執行此操作:如果您將滑塊拖離滑塊然后向兩側移動,則視頻移動的速度會發生變化。 離滑塊越遠,它越精確/越少。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.