10

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!

kcharwood
  • 2,501
  • 19
  • 22

1 Answers1

14

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];
Jeff Youel
  • 653
  • 4
  • 7
  • That was it. The AVFoundation docs show how to set this up on iOS by subclassing a UIView. However, NSView does not have layerClass, which is one reason it wasn't working on my end. So I subclassed a different way, and made sure to add the setWantsLayer, and it seems to work great! – kcharwood Oct 21 '11 at 12:02
  • 4
    On iOS, doesn't look like there's a "wantslayer" attribute/method – zakdances Mar 28 '13 at 01:15
  • 1
    Setting the frame did the trick for me. Xcode doesn't recognize `setWantsLayer`. – John Erck Nov 09 '14 at 23:22