在AVPlayer中准确地寻找,而不是短于两秒 [英] Seeking accurately, as opposed to two seconds short, in AVPlayer

查看:97
本文介绍了在AVPlayer中准确地寻找,而不是短于两秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Cocoa应用程序中使用AVPlayer,并且实现了跳转到视频结尾的命令.

I'm using AVPlayer in a Cocoa app, and I've implemented a command that jumps to the end of the video.

问题是,AVPlayer并没有寻找我告诉它的地方.

The problem is, AVPlayer doesn't seek to where I told it to.

例如,我拥有的一部视频长4分14秒.当我寻求结束时,AVPlayer寻求4分12秒-短2秒.如果我随后点击播放,播放器将播放两秒钟,然后然后到达结尾.

For example, one of the videos I have is 4 minutes and 14 seconds long. When I seek to the end, AVPlayer seeks to 4 minutes and 12 seconds—two seconds short. If I then hit play, the player will play for two seconds, then reach the end.

我的第一次尝试是

[self.player seekToTime:self.player.currentItem.duration];

我已将其切换为:

[self.player seekToTime:self.player.currentItem.duration
        toleranceBefore:kCMTimePositiveInfinity
         toleranceAfter:kCMTimeZero];

没有一个比另一个更好.

Neither one works any better than the other.

我也尝试过尝试kCMTimePositiveInfinity.那时它只是不理我.

I also tried seeking to kCMTimePositiveInfinity. It just ignored me then.

播放器是否已加载视频的那一部分似乎并不重要.我什至可以寻找到不尽人意的地方,一直玩到真正的尽头,然后再次尝试寻找到尽头,然后它会跳回到不尽人意的地方.

It doesn't seem to matter whether the player has loaded that part of the video yet. I can even seek to the not-quite-end, play through to the true end, then try again to seek to the end, and it will jump back to the not-quite-end.

不足之处并不总是两秒钟.在我的某些视频中,它或多或少正确地起作用,如果不完全正确,则跳到非常接近真实的尽头.至少有一个,它缩短了三秒钟.长度似乎不是一个因素.所有这些长度都差不多,除了一个长度,它长一个小时以上,而且要短两秒钟.

The shortfall is not always two seconds. On some of my videos, it works more or less exactly correctly, jumping very close to the true end if not exactly to it. On at least one, it goes to three seconds short. Length does not seem to be a factor; all of these are about the same length, except for one, which is over an hour long and seeks to two seconds short.

那么,为什么AVPlayer会比我告诉的地方跳三秒之久,又如何说服它跳到我要求的那一刻呢?

So, why is AVPlayer jumping as many as three seconds short of where I told it to, and how do I convince it to jump to the moment I asked for?

推荐答案

我误解了公差,并将公差倒退了.

I misunderstood the tolerances and had them backwards.

每个容差是允许AVPlayer在该方向上偏离请求时间的距离.因此,为toleranceBefore传递正无穷大就是说你可以尽早出现",而为toleranceAfter传递零就意味着就是不要迟到"(当寻求结束).

Each tolerance is how far in that direction AVPlayer is allowed to move away from the requested time. So, passing positive infinity for toleranceBefore is saying "you can be as early as you want", and passing zero for toleranceAfter is saying "just don't be late" (which would be a neat trick when seeking to the end).

因此,解决方案是切换值:

Thus, the solution is to switch the values:

[self.player seekToTime:self.player.currentItem.duration
        toleranceBefore:kCMTimeZero
         toleranceAfter:kCMTimePositiveInfinity];

或者用英语:不要早,但是你可以根据需要迟到."

Or, in English: "Don't be early, but you can be as late as you want."

这篇关于在AVPlayer中准确地寻找,而不是短于两秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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