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!
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With