使AVPlayer充满整个屏幕 [英] make AVPlayer fill the entire screen

查看:161
本文介绍了使AVPlayer充满整个屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用,希望在单个视图的背景下播放视频.

I have an app where I'd like a video to play in the background of a single view.

我所做的是创建一个AVPlayer,将其添加到AVPlayerLayer并将其作为子层添加到我的视图中.

What I did is create an AVPlayer, add that to an AVPlayerLayer and add that as a sublayer to my view.

然后设置框架和videoGravity属性:

self.player = AVPlayer(url: URL(fileURLWithPath: path))
let playerLayer = AVPlayerLayer(player: self.player)
self.layer.addSublayer(playerLayer)
playerLayer.frame = self.bounds
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill

现在可以播放视频了,但是它不能充满整个屏幕.它会填满原始大小,但不会拉伸/缩放.

Now the video plays, except it doesn't fill the entire screen. It fills it's original size, but it doesn't stretch/scale.

我错过了什么吗?为什么我的视频没有调整大小?

Am I missing something? Why is my video not resizing?

谢谢.

我添加了一个屏幕截图,并将视图设置为紫色,以便于参考.

I added a screenshot and made the view purple for easier reference.

推荐答案

在viewDidLayoutSubviews()内定义playerLayer.frame

Define the playerLayer.frame inside viewDidLayoutSubviews()

在定义帧时,尚未计算视图的最终帧.

At the time you are defining the frames the final frames for the views were not yet calculated.

这篇关于使AVPlayer充满整个屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆