OStack程序员社区-中国程序员成长平台

标题: ios - AVPlayer 视频不适合水平 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:21
标题: ios - AVPlayer 视频不适合水平

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

下面是我正在使用的代码

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

提前感谢您



Best Answer-推荐答案


当调用 'viewDidLoad' 时, View 仍然可能有不正确的大小。所以你的 PlayerLayer 也会有不正确的大小。尝试将您的代码移动到“viewDidLayoutSubviews”函数:

override func viewDidLayoutSubviews() 
{
    super.viewDidLayoutSubviews()

    // Set up your player layer instance here
}

关于ios - AVPlayer 视频不适合水平,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45288548/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4