构建我的第一个应用并尝试将背景视频水平放置。
下面是代码l' m
override func viewDidLoad()
{
super.viewDidLoad()
let URL = Bundle.main.url(forResource: "homedocapp", withExtension: "mp4")
Player = AVPlayer.init(url: URL!)
PlayerLayer = AVPlayerLayer(player: Player)
PlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
PlayerLayer.frame = self.view.frame; PlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
PlayerLayer.zPosition = -1
Player.actionAtItemEnd = AVPlayerActionAtItemEnd.none
self.view.layer.addSublayer(PlayerLayer)
Player.play()
view.layer.insertSublayer(PlayerLayer, at: 0)
NotificationCenter.default.addObserver(self, selector: #selector(playerItemReachEnd(notification:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: Player.currentItem)
}
谢谢你的进步
答案 0 :(得分:0)
调用'viewDidLoad'时,视图的大小仍然不正确。所以你的PlayerLayer也会有不正确的大小。尝试将代码移动到'viewDidLayoutSubviews'函数:
override func viewDidLayoutSubviews()
{
super.viewDidLayoutSubviews()
// Set up your player layer instance here
}
答案 1 :(得分:0)
在viewDidLoad中初始化时,您必须使用约束调整播放器的大小,导致self.view.frame不正确。
答案 2 :(得分:0)
旋转时,我正在分屏。
class ViewController: UIViewController {
var Player: AVPlayer!
var PlayerLayer: AVPlayerLayer!
override func viewDidLayoutSubviews()
{
super.viewDidLayoutSubviews()
let URL = Bundle.main.url(forResource: "homedocapp", withExtension: "mp4")
Player = AVPlayer.init(url: URL!)
PlayerLayer = AVPlayerLayer(player: Player)
PlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
PlayerLayer.frame = view.layer.frame
Player.actionAtItemEnd = AVPlayerActionAtItemEnd.none
Player.play()
view.layer.insertSublayer(PlayerLayer, at: 0)
NotificationCenter.default.addObserver(self, selector: #selector(playerItemReachEnd(notification:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: Player.currentItem)
}
努力学习