make AVPlayer fill the entire screen

泪湿孤枕 提交于 2019-12-10 22:11:25

问题


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

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

Then I set the frame and the videoGravity property:

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?

Thanks.

Edit:

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


回答1:


Define the playerLayer.frame inside viewDidLayoutSubviews()

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




回答2:


If you want to video to be stretch on the entire view, just delete the line

playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill

videoGravity default value is AVLayerVideoGravityResize



来源:https://stackoverflow.com/questions/48509972/make-avplayer-fill-the-entire-screen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!