在两个视图上同时显示 AVPlayer 内容
我正在为 Mac 创建一个 HTTP Live Streaming 客户端,它将控制大屏幕上的视频播放。我的目标是在主屏幕上有一个控制用户界面,在辅助屏幕上有全屏视频。
使用 AVFoundation,我已经成功地能够打开流并从我的控制 UI 控制它的各个方面,并且我现在尝试在辅助屏幕上复制视频。事实证明这比我想象的要困难......
在控制屏幕上,我有一个 AVPlayerLayer 正在显示来自 AVPlayer 的视频内容。我的目标是创建另一个 AVPlayerLayer,并向其发送相同的播放器,以便两个播放器同时在两个不同的视图中播放相同的视频。然而,这是行不通的。
深入挖掘后,我在 AVFoundation 文档中发现了这一点:
您可以使用相同的 AVPlayer 对象创建任意数量的播放器层。只有最近创建的播放器层才会真正在屏幕上显示视频内容。
这实际上对我来说没有用,因为我需要视频在两个视图中正确显示。
我可以从同一个 AVAsset 创建一个新的 AVPlayerItem 实例,然后创建一个新的 AVPlayer 并将其添加到新的 AVPlayerLayer 并显示视频,但它们不再同步,因为它们是两个不同的播放器,生成两个不同的音频流同一流的不同部分。
有人对如何将相同的 AVPlayer 内容放入两个不同的视图有任何建议吗?也许是某种 CALayer 镜像技巧?
I am creating an HTTP Live Streaming Client for Mac that will control video playback on a large screen. My goal is to have a control UI on the main screen, and full screen video on the secondary screen.
Using AVFoundation, I have successfully been able to open the stream and control all aspects of it from my control UI, and I am now attempting to duplicate the video on a secondary screen. This is proving more difficult than I imagined...
On the control screen, I have an AVPlayerLayer that is displaying the video content from an AVPlayer. My goal was to create another AVPlayerLayer, and send it the same player so that both players are playing the same video at the same time in two different views. However, that is not working.
Digging deeper, I found this in the AVFoundation docs:
You can create arbitrary numbers of player layers with the same AVPlayer object. Only the most-recently-created player layer will actually display the video content on-screen.
This is actually useless to me, because I need the video showing correctly in both views.
I can create a new instance of AVPlayerItem from the same AVAsset, then create a new AVPlayer and add it to a new AVPlayerLayer and have video show up, but they are no longer in sync because they are two different players generating two different audio streams playing different parts of the same stream.
Does anyone have any suggestions on how to get the same AVPlayer content into two different views? Perhaps some sort of CALayer mirroring trick?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AVSyncronizedLayer 可能会有所帮助。我以不同的方式使用它(同步两个不同的媒体对象而不是同一个),但原则上应该可以加载相同的项目两次,然后使用 AvSyncronized 层来保持它们同步。
AVSyncronizedLayer may help. I'm using it differently (to syncronize two different media objects rather than the same one) but in principle it should be possible to load the same item twice and then use an AvSyncronized layer to keep them synced.
Apple 的文档现在声明了这一点:
文档链接
这确实适用于我的应用程序以及。
Apple's docs now state this:
link to docs
This does indeed work in my app as well.
我发现这个话题已经很老了,但我认为它仍然会有帮助。你写的
但是,它正在发挥作用。我刚刚在我的项目中尝试过。这是我的层初始化代码:
这是我得到的:
如您所见,有两个 AVPlayerLayers 非常完美地同步播放同一个 AVPlayerItem
I see that this topic got very old, but I think it still would be helpful. You wrote that
But, it's working. I just tried it in my project. Here's my code of layer initializations:
And here's what I got:
As you can see, there're two AVPlayerLayers playing the same AVPlayerItem in the very perfect sync