构建我的第一个应用程序,并尝试将背景视频水平放置。

下面是我正在使用的代码

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)


}

AVPlayer Horizontal display

预先谢谢你

最佳答案

调用“viewDidLoad”时,视图的尺寸仍然可能不正确。因此,您的PlayerLayer也将具有不正确的尺寸。尝试将代码移至“viewDidLayoutSubviews”函数:

override func viewDidLayoutSubviews()
{
    super.viewDidLayoutSubviews()

    // Set up your player layer instance here
}

07-28 02:36