AVPlayer 视频不适合水平

AVPlayer video is not fitting horizontal

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

下面是我正在使用的代码

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
}

你必须在 viewDidLoad 初始化时调整播放器的大小,因为 self.view.frame 不正确。

Spilt screen

旋转时出现分屏。

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)


}

努力学习