菜鸟教程小白 发表于 2022-12-11 18:21:39

ios - AVPlayer 视频不适合水平


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

<p>下面是我正在使用的代码</p>

<pre><code>override func viewDidLoad()
{
    super.viewDidLoad()

    let URL = Bundle.main.url(forResource: &#34;homedocapp&#34;, withExtension: &#34;mp4&#34;)

    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)


}
</code></pre>

<p> <a href="/image/Mhaow.png" rel="noreferrer noopener nofollow">AVPlayer Horizontal display</a> </p>

<p>提前感谢您</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>当调用 'viewDidLoad' 时, View 仍然可能有不正确的大小。所以你的 PlayerLayer 也会有不正确的大小。尝试将您的代码移动到“viewDidLayoutSubviews”函数:</p>

<pre><code>override func viewDidLayoutSubviews()
{
    super.viewDidLayoutSubviews()

    // Set up your player layer instance here
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - AVPlayer 视频不适合水平,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45288548/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45288548/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - AVPlayer 视频不适合水平