问题
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