stackOverFlew
stackOverFlew

Reputation: 1499

resize AVPlayerItem container view based on aspect ratio from video

I have a container UIView called videoView. I add a AVPLayerLayer to that view. I do not know how to resize the videoView to be of the same proportions as the avplayer layer. Using videoGravity does not have the desired effect.

AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:path];

_videoPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem];

AVPlayerLayer *layer = [AVPlayerLayer layer];

[_avplayerLayer setPlayer:_videoPlayer];
[_avplayerLayer setBackgroundColor:[UIColor whiteColor].CGColor];
[_avplayerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[_avplayerLayer setFrame:_videoView.bounds];
[_videoView.layer addSublayer:_avplayerLayer];

I am using autolayout, and my layout would accommodate a change in the height of _videoView.

Upvotes: 2

Views: 4055

Answers (1)

Casey
Casey

Reputation: 6701

videoGravity will not change the frame of the view, instead it fills the current frame with the video.

i think you should inspect each video's resolution before playing it and adjust videoView's height constraint (which should be an ivar and constrained to videoView's width) multiplier. videoView width should be constrained to the screen width.

Upvotes: 3

Related Questions