iOS AVPlayer取消缓冲

iOS AVPlayer cancel buffering

如果我使用

主机的文件加载 AVPlayer
[AVPlayer playerWithPlayerItem:playerItem];

而且“缓冲”花费的时间比用户想要的要长,我该如何让他们取消它?

我认为没有直接停止缓冲的方法。但是您可以检查 AVPlayerItem 的布尔值 playbackLikelyToKeepUp 属性 以检查缓冲区是否正在加载。然后我没有在 AVPlayer 上暂停播放。

所以正确答案是@Che:

If you will call [self.player replaceCurrentItemWithPlayerItem:nil]; buffering stops. – Che Oct 13 at 14:36

并且您在侦听来自 playerWithPlayerItem 的状态更新的观察者上执行此操作。谢谢大家!

Swift 3

player?.replaceCurrentItem(with: nil)

你可以试试这个

// pause buffer
[self.playerItem cancelPendingSeeks];
[self.playerItem.asset cancelLoading];

您需要管理 avplayer currentItem 的 preferredForwardBufferDuration。如果你想停止缓冲将值设置为 1 因为如果你将它设置为 0 它将自动设置

self.avPlayer.currentItem.preferredForwardBufferDuration = 1;

来自 Apple 文档: 属性 定义了首选的转发缓冲持续时间(以秒为单位)。如果设置为 0,播放器将为大多数用例选择适当的缓冲级别。将此 属性 设置为较低的值将增加播放停止和重新缓冲的机会,而将其设置为较高的值将增加对系统资源的需求。