CodexBloom - Programming Q&A Platform

Handling AVPlayer Layer Resize Properly in Objective-C on iOS 16

👀 Views: 59 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-13
iOS AVFoundation AVPlayer Objective-C

I'm integrating two systems and I'm stuck on something that should probably be simple. I've been struggling with this for a few days now and could really use some help. I'm currently experiencing an scenario where the AVPlayerLayer does not resize correctly when the orientation of the device changes. I'm using the latest iOS 16 version, and the player is embedded within a UIViewController. The question arises specifically after changing the device orientation; the video still displays in its original size and doesn't fill the new dimensions of its parent view. I've set up the AVPlayerLayer as follows: ```objective-c - (void)viewDidLoad { [super viewDidLoad]; self.player = [AVPlayer playerWithURL:videoURL]; self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player]; self.playerLayer.frame = self.view.bounds; self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspect; [self.view.layer addSublayer:self.playerLayer]; } ``` After the orientation change, I'm trying to update the frame of the player layer in `viewDidLayoutSubviews`: ```objective-c - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; self.playerLayer.frame = self.view.bounds; } ``` However, when I run the app and rotate the device, the video still isn't resizing as expected, and I see a portion of the video layer cut off. I've confirmed that the view's bounds are updating correctly during this call, but the layer remains exploring in its previous size. I've also tried setting the `videoGravity` property to `AVLayerVideoGravityResize`, but that didn't resolve the scenario. Is there something specific I might be missing when handling the resizing of AVPlayerLayer, or is there a better approach I should consider for managing the orientation changes in iOS 16? Am I missing something obvious? Any ideas what could be causing this? I'm on Ubuntu 22.04 using the latest version of Objective-C.