Fullscreen: AVPlayerLayer is not resized when resizing its parent view

14,788

Solution 1

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

Solution 2

In case you add AVPlayerLayer as sublayer, you need to update its frame

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    self.avPlayerLayer.frame = self.movieContainerView.bounds;
}
Share:
14,788
nicolas
Author by

nicolas

merge keep

Updated on June 04, 2022

Comments

  • nicolas
    nicolas almost 2 years

    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?

  • jose920405
    jose920405 over 8 years
    bad idea. Because is a player, each time when indicator time is updated this function in dispatched
  • Dalmazio
    Dalmazio over 8 years
    What if you need to add as a sublayer? For example, using a layer-hosted view that contains many sublayers, including an AVPlayerLayer.
  • morgman
    morgman about 8 years
    Apple updated their AVPlayer example with a version that demonstrates how to implement this using swift: developer.apple.com/library/prerelease/ios/samplecode/…
  • iluvcapra
    iluvcapra over 7 years
    Any idea how to do this with an NSView? NSView doesn't have layerClass
  • Jin
    Jin over 7 years
    @iluvcapra On OS X, CALayer has autoresizingMask property and NSView can resize sublayers automatically. So for resizing, you can just add player's layer as sublayer of a NSView and set the autoresizingMask to kCALayerWidthSizable|kCALayerHeightSizable. Another step is turn on core animations support for that view: Check the checkbox in View Effects inspector for nib file, or set wantsLayer property to YES programatically. see developer.apple.com/library/ios/documentation/Cocoa/Conceptu‌​al/…
  • Genevios
    Genevios about 6 years
    Good answer, for me very helped
  • Joel
    Joel almost 4 years
    this doesn't work on iOS 13. the player layer remains at the orig size