Fullscreen: AVPlayerLayer is not resized when resizing its parent view

nicolas picture nicolas · Feb 29, 2012 · Viewed 14.4k times · Source

I am developing a video player with the AVPlayer API from AV Foundation in MonoTouch (but a solution in objective-c could be nice too). I am trying to implement a fullscreen mode.

To display the video frames, I have a UIView (let's call it playback view) where I added the AVPlayerLayer as subview of the playback view layer:

UIView *playbackView = ...
AVPlayerLayer *playerLayer = ...
[playbackView.layer addSublayer:playerLayer];

the layer properties are set like that:

playerLayer.needsDisplayOnBoundsChange = YES;
playbackView.layer.needsDisplayOnBoundsChange = YES;

to be sure that they are resized if the playback view size is changed. Initially, the playback view has the coordinates (0, 0, 320, 180) (only displayed at the top of the UI). Then, I am doing a fullscreen animation by setting the playback view frame size as being the window size:

playbackView.frame = playbackView.window.bounds;

It's works fine. The playback view is now filling all the window (set a background color to see it). But my AVPlayerLayer is still staying at the top of the view like previously in non-fullscreen mode.

Why the AVPlayer layer is not resized according to the playback view new size? Do I need to make an animation on the AVPlayerLayer as well? A refresh with setNeedsLayout, layoutSubviews (it doesn't seems to change something...)? Remove the AVPlayerLayer and add it again?

Answer

Jin picture Jin · Apr 10, 2012

Don't add as sublayer. Just use playbackView's layer as AVPlayerLayer, like this:

+ (Class)layerClass {
    return [AVPlayerLayer class];
}
- (AVPlayer*)player {
    return [(AVPlayerLayer *)[self layer] player];
}
- (void)setPlayer:(AVPlayer *)player {
    [(AVPlayerLayer *)[self layer] setPlayer:player];
}

See AV Foundation Programming Guide - The Player View