I'm trying to create a view hierarchy similar to what you'd expect in a media playback viewer like the QuickTime Player:
+ Host View
+ Video Controls (NSView layer-backed)
+ Video View (NSView layer-hosted)
+ AVPlayerLayer
Since layer-hosted views cannot contain subviews, the video controls view is a sibling of the video view and simply ordered-front so that it's on top of the video view.
This current view hierarchy appears to be working fine for me, but I remain a bit confused over whether it's officially "supported" because of the overlapping, sibling views (the video controls view always overlaps the video view).
This Stack Overflow question: Is there a proper way to handle overlapping NSView siblings? offers conflicting information regarding overlapping sibling views.
I would assume the more 'correct' way to handle this is for the video controls to be a subview of the video view, which is only possible of I change the video view from being a layer-hosted view to a layer-backed view.
By default a layer-backed view uses a basic CALayer
as its backing store, but NSView
exposes makeBackingLayer
to allow you to return a custom layer such as an AVPlayerLayer
.
By doing that, and moving the controls view to be a subview of this layer-backed video view, things also appear to work properly but now there's an AVPlayer
object that is directly modifying the contents of the AVPlayerLayer
. That seems to be contrary to the requirement that in a layer-backed view, you should never modify the contents of the layer without going through the NSView
using something like drawRect
or updateLayer
.
This seems to leave me with two choices, neither of which appears 'correct' based on my interpretation of the document:
Option 1:
- Layer-hosted view for the
AVPlayerLayer
- Overlapping sibling view for the controls view.
Option 2:
- Layer-back view with an
AVPlayerLayer
viamakeBackingLayer
AVPlayer
that directly modifies the contents of theAVPlayerLayer
- Controls view as a subview of the video view
I'm inclined to think that option #2 is the more correct way and that in this scenario, it's OK for the AVPlayer
to directly modify the contents of the AVPlayerLayer
even though it's in a layer-backed view, but I'm not certain and would be curious if others have any thoughts or experiences with a setup like this.