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()
}
}
Finally I get it using:
playerLayer.videoGravity = AVLayerVideoGravity.resize