根据视频的宽高比调整 AVPlayerItem 容器视图的大小

resize AVPlayerItem container view based on aspect ratio from video

我有一个名为 videoView 的容器 UIView。我向该视图添加了一个 AVPLayerLayer。我不知道如何将 videoView 调整为与 avplayer 层相同的比例。使用 videoGravity 没有达到预期的效果。

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];

我正在使用自动版式,我的版式会适应 _videoView 的高度变化。

videoGravity 不会改变视图的帧,而是用视频填充当前帧。

我认为你应该检查每个视频的 resolution before playing it and adjust videoView's height constraint (which should be an ivar and constrained to videoView's width) multipliervideoView 宽度应限制为屏幕宽度。