AVQueuePlayer是否支持异步添加AVPlayerItems?

发布于 2024-12-01 10:55:22 字数 463 浏览 6 评论 0原文

我有一个视频播放器,需要播放网络上的一系列视频。这些视频的 URL 事先未知,因为它们来自其他 HTTP 请求的 XML 或 JSON 响应。

当视频 URL 已知时,我创建 AVPlayerItems 并将它们添加到 AVQueuePlayer 中。我观察播放器状态和项目状态以确定正在播放哪个项目等。

问题是播放器似乎进入各种奇怪的状态,它返回 AVFoundationErrorDomain 错误 -11800 ,从那时起播放器拒绝播放任何内容。即使在取消分配 AVQueuePlayer 并启动新的播放器后,之前播放的视频仍会拒绝播放。

将新的 AVPlayerItems 添加到正在播放的 AVQueuePlayer 是受支持的操作,还是我应该以其他方式执行此操作?如果支持,我应该注意/做什么(例如线程问题?KVO 问题?)才能使这项工作正常进行?

I have a video player that needs to play a sequence of videos from the network. The URLs for these videos are not known in advance, as they come from XML or JSON responses from other HTTP requests.

As the video URLs become known, I create AVPlayerItems and add them to an AVQueuePlayer. I observe the player status and the item status to determine which item is playing etc.

The problem is that the player seems to get into various weird states where it returns AVFoundationErrorDomain error -11800 and from then on the player refuses to play anything. Even after deallocating the AVQueuePlayer and starting a new one, videos that previously played refuse to play.

Is adding new AVPlayerItems to a playing AVQueuePlayer a supported operation, or should I be doing this another way? If it is supported, what should I be looking out for/doing (Eg. thread issues? KVO issues?) to make this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ヅ她的身影、若隐若现 2024-12-08 10:55:22

答案是肯定的,AVQueuePlayer 确实可以异步添加 AVPlayerItems。重要的部分在文档中:

AVPlayer 在调度队列上序列化播放期间动态发生的更改的通知。默认情况下,该队列是主队列(请参阅dispatch_get_main_queue)。为了确保安全访问播放器的非原子属性,同时可能会报告播放状态的动态变化,您必须使用接收器的通知队列来序列化访问。通常情况下,这种序列化自然是通过在主线程或队列上调用AVPlayer的各种方法来实现的。

由于 KVO 观察者可能会在其他线程上触发,因此从主线程调用 AVPlayer(和 AVQueuePlayer)上的所有方法至关重要。

如果不这样做,当事情进入不一致的状态时,您会看到奇怪的行为和 AVFoundationErrorDomain -11800

The answer is yes, AVQueuePlayer does work with adding AVPlayerItems asynchronously. The important part is in the documentation:

AVPlayer serializes notifications of changes that occur dynamically during playback on a dispatch queue. By default, this queue is the main queue (see dispatch_get_main_queue). To ensure safe access to a player’s nonatomic properties while dynamic changes in playback state may be reported, you must serialize access with the receiver’s notification queue. In the common case, such serialization is naturally achieved by invoking AVPlayer’s various methods on the main thread or queue.

Since KVO observers might fire on other threads, it's critical to call all methods on AVPlayer (and AVQueuePlayer) from the main thread.

If you don't, you'll see the weird behaviour and AVFoundationErrorDomain -11800 as things get into inconsistent states

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文