Displaying AVPlayer content on two views simultaneously

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?



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:

在这里输入图像描述

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

链接地址: http://www.djcxy.com/p/9770.html

上一篇: 删除了migration.rb文件? 轨道中的潜在错误?

下一篇: 同时在两个视图中显示AVPlayer内容