在两个视图上同时显示 AVPlayer 内容

发布于 2024-12-11 08:17:56 字数 690 浏览 0 评论 0原文

我正在为 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 技术交流群。

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

发布评论

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

评论(3

没企图 2024-12-18 08:17:56

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.

月下伊人醉 2024-12-18 08:17:56

Apple 的文档现在声明了这一点:

您可以使用同一 AVPlayer 对象创建任意数量的播放器层,但您应该限制创建的层数,以避免影响播放性能。

文档链接

这确实适用于我的应用程序以及。

Apple's docs now state this:

You can create arbitrary numbers of player layers with the same AVPlayer object, but you should limit the number of layers you create to avoid impacting playback performance.

link to docs

This does indeed work in my app as well.

待"谢繁草 2024-12-18 08:17:56

我发现这个话题已经很老了,但我认为它仍然会有帮助。你写的

我有一个 AVPlayerLayer,它显示来自 AVPlayer 的视频内容。我的目标是创建另一个 AVPlayerLayer,并向其发送相同的播放器,以便两个播放器同时在两个不同的视图中播放相同的视频。然而,这不起作用。

但是,它正在发挥作用。我刚刚在我的项目中尝试过。这是我的层初始化代码:

AVPlayerLayer *playerLayer = [AVPlayerLayer new];
    [playerLayer setPlayer:_testPlayer];

    playerLayer.frame = CGRectMake(0, 0, _videoView.frame.size.width, _videoView.frame.size.height);
    playerLayer.contentsGravity = kCAGravityResizeAspect;
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;

    _defaultTransform = playerLayer.affineTransform;

    [_videoView.layer insertSublayer:playerLayer atIndex:0];

    AVPlayerLayer *testLayer_1 = [AVPlayerLayer playerLayerWithPlayer:_testPlayer];
    testLayer_1.frame = CGRectMake(100, 100, 200, 200);
    testLayer_1.contentsGravity = kCAGravityResizeAspect;
    testLayer_1.videoGravity = AVLayerVideoGravityResizeAspect;

    [_videoView.layer insertSublayer:testLayer_1 atIndex:1];

这是我得到的:

在此处输入图像描述

如您所见,有两个 AVPlayerLayers 非常完美地同步播放同一个 AVPlayerItem

I see that this topic got very old, but I think it still would be helpful. You wrote that

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.

But, it's working. I just tried it in my project. Here's my code of layer initializations:

AVPlayerLayer *playerLayer = [AVPlayerLayer new];
    [playerLayer setPlayer:_testPlayer];

    playerLayer.frame = CGRectMake(0, 0, _videoView.frame.size.width, _videoView.frame.size.height);
    playerLayer.contentsGravity = kCAGravityResizeAspect;
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;

    _defaultTransform = playerLayer.affineTransform;

    [_videoView.layer insertSublayer:playerLayer atIndex:0];

    AVPlayerLayer *testLayer_1 = [AVPlayerLayer playerLayerWithPlayer:_testPlayer];
    testLayer_1.frame = CGRectMake(100, 100, 200, 200);
    testLayer_1.contentsGravity = kCAGravityResizeAspect;
    testLayer_1.videoGravity = AVLayerVideoGravityResizeAspect;

    [_videoView.layer insertSublayer:testLayer_1 atIndex:1];

And here's what I got:

enter image description here

As you can see, there're two AVPlayerLayers playing the same AVPlayerItem in the very perfect sync

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