gpt4 book ai didi

ios - AVPlayer 使用平移手势快进/后退

转载 作者:行者123 更新时间:2023-12-01 22:27:03 30 4
gpt4 key购买 nike

在过去的两天里,我一直在用一个恼人的小故障敲我的头,希望有人能解释一下。
基本设置:我有一个带有播放器的 AVPlayerLayer, View 中有一个平移手势识别器,我希望用户能够来回滑动手指,视频会相应地搜索。
收获:我想如果用户抬起手指并再次将其放置在屏幕上的任何位置,以便在 处恢复快进或快退。准确 它停止的同一帧并从那里继续前进。
我看过这两个问题:
Pan Gesture with AVPlayer
Pan to seek AVPlayer
我在这里尝试过 Apple 的建议:https://developer.apple.com/library/content/qa/qa1820/_index.html
同样,但问题是每当我开始一个新的平移手势时,播放器都会跳几帧然后继续。
我的最新方法是在查找完成 block 完成后设置当前时间,然后我尝试将新的查找时间附加到该时间。
这是我的设置:

self.item = [AVPlayerItem playerItemWithURL:resource];
self.player = [AVPlayer playerWithPlayerItem:self.item];
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];

[self.view.layer addSublayer:self.playerLayer];
self.playerLayer.frame = self.view.bounds;

UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];

[self.view addGestureRecognizer:recognizer];
我的手势识别器处理:
- (void)swipe:(UIPanGestureRecognizer *)paramRecognizer
{
switch(paramRecognizer.state) {
// case UIGestureRecognizerStateBegan:
case UIGestureRecognizerStateChanged:
{
[self.player pause];

CGPoint translation = [paramRecognizer translationInView:self.view];

float horizontalTranslation = translation.x;

float durationInSeconds = CMTimeGetSeconds(self.player.currentItem.asset.duration);

//I'd like to be able to swipe across the whole view.
float translationLimit = self.view.bounds.size.width;
float minTranslation = 0;
float maxTranslation = translationLimit;

if (horizontalTranslation > maxTranslation) {
horizontalTranslation = maxTranslation;
}

if (horizontalTranslation < minTranslation) {
horizontalTranslation = minTranslation;
}

float timeToSeekTo = [self normalize:horizontalTranslation
andMinDelta:minTranslation
andMaxDelta:maxTranslation
andMinDuration:0
andMaxDuration:durationInSeconds];

if(CMTIME_IS_VALID(self.currentTime)){
float seconds = self.currentTime.value/self.currentTime.timescale;

[self.player seekToTime:CMTimeMakeWithSeconds(seconds+timeToSeekTo, self.player.currentTime.timescale)
toleranceBefore:kCMTimeZero
toleranceAfter:kCMTimeZero completionHandler:^(BOOL finished)
{
if(finished)
self.currentTime = self.player.currentItem.currentTime;
}];
}
else
{
[self.player seekToTime:CMTimeMakeWithSeconds(timeToSeekTo,
self.player.currentTime.timescale) toleranceBefore:kCMTimeZero
toleranceAfter:kCMTimeZero completionHandler:^(BOOL finished) {
if(finished)
self.currentTime = self.player.currentItem.currentTime;
}];
}

}
break;
}
}
规范化方法是这样的:
- (float)normalize:(float)delta
andMinDelta:(float)minDelta
andMaxDelta:(float)maxDelta
andMinDuration:(float)minDuration
andMaxDuration:(float)maxDuration
{
float result = ((delta - minDelta) * (maxDuration - minDuration) / (maxDelta - minDelta) + minDuration);
return result;
}
任何帮助将不胜感激!

最佳答案

UIGestureRecognizerStateBegan保存 CMTime通过 AVPlayer然后进行增量更改平移,然后在 UIGestureRecognizerStateEnded只是寻求回到原来节省的时间?

只是为了更顺畅地寻找注意不要暂停视频,将速率设置为 0。

关于ios - AVPlayer 使用平移手势快进/后退,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41853654/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com