我跟随https://stackoverflow.com/a/46337372/2139691,以便填补背景。所以我使用一个名为myview的视图,蓝色背景,但是当我添加一个视频图层时,我的结果很糟糕。
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()
}
}
默认情况下,播放器图层背景是透明的。如果要隐藏后面的蓝色视图,请尝试在播放器图层上设置背景颜色。
playerLayer.backgroundColor = UIColor.black.cgColor
最后我得到它使用:
playerLayer.videoGravity = AVLayerVideoGravity.resize