0

I've following https://stackoverflow.com/a/46337372/2139691 in order to have a view to fill the background. So I use a View called myview with a blue background, but when I add a video layer I have bad results.

class MovViewController: UIViewController {

    var avPlayer: AVPlayer!
    @IBOutlet weak var myview: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let filepath: String? = Bundle.main.path(forResource: "video_fondo", ofType: "mp4")
        let fileURL = URL.init(fileURLWithPath: filepath!)

        let player = AVPlayer(url: fileURL)
        let playerLayer = AVPlayerLayer(player: player)

        playerLayer.frame = myview.bounds
        myview.layer.insertSublayer(playerLayer, at: 0)
        player.play()

    }
}

The result: enter image description here

Ricardo
  • 7,921
  • 14
  • 64
  • 111

2 Answers2

1

The player layer background is transparent by default. Try setting a background color on the player layer if you want to hide the blue view behind.

playerLayer.backgroundColor = UIColor.black.cgColor
1

Finally I get it using:

playerLayer.videoGravity = AVLayerVideoGravity.resize
Ricardo
  • 7,921
  • 14
  • 64
  • 111