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.
If you want to video to be stretch on the entire view, just delete the line
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
videoGravity
default value is AVLayerVideoGravityResize
playerLayer.videoGravity = .resizeAspectFill
Define the playerLayer.frame inside viewDidLayoutSubviews()
At the time you are defining the frames the final frames for the views were not yet calculated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With