Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get AVPlayerLayer to display video in NSView

I am trying to make a simple HLS player to control playback and display on a secondary monitor. I am using AVFoundation on in 10.7 to control the playback. I can successfully create the AVPlayerItem and the AVPlayer, but I am having problems actually getting the video to display within an NSView.

I must confess I am a Cocoa novice, and am coming from iOS development, so I may be missing something simple. However I have spent 4-5 hours trying to get this to work, and I have been unsuccessful.

When I play the video from the AVPlayer, playback begins and I can hear audio. However, no video is showing up.

I have tried to make it as simple as possible. I have a NSViewController, where I add a AVPlayerLayer to it's view's layer:

AVPlayerLayer * playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
[playerLayer setFrame:self.view.bounds];
[self.view.layer addSublayer:playerLayer];

As far as I know, that is all I have to do. However, video never appears within the view.

Has anyone had success adding a AVPlayerLayer to a NSView? The AVFoundation documentation shows how this can be done with a UIView, and I have tried that method as well with no luck in NSView.

Any help would be greatly appreciated!

like image 278
kcharwood Avatar asked Oct 20 '11 14:10

kcharwood


1 Answers

Try sending setWantsLayer:YES to your view before adding the sublayer.

[self.view setWantsLayer:YES];
AVPlayerLayer * playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
[playerLayer setFrame:self.view.bounds];
[self.view.layer addSublayer:playerLayer];
like image 79
Jeff Youel Avatar answered Oct 31 '22 21:10

Jeff Youel