AVPlayer层有时不显示视频内容

时间:2016-11-28 11:53:57

标签: ios swift avplayer avplayerlayer

AVPlayer图层有时不显示视频内容但播放音频。 这种情况有时不会发生 这是我的代码行:

 override func viewDidLoad() {
        super.viewDidLoad()
        self.tempVideoPath = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("tmpMov.mov")
        self.player = AVPlayer(url: self.tempVideoPath!)
}

 override func viewDidAppear(_ animated: Bool) {
     super.viewDidAppear(animated)
     self.playerLayer = AVPlayerLayer(player: self.player)
     self.playerLayer?.frame = self.videoPlayer.bounds
     self.playerLayer?.backgroundColor = UIColor.yellow.cgColor
     self.videoPlayer.layer.addSublayer(playerLayer!)
     print("player = \(playerLayer?.bounds)")

 }

帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

您的代码存在的问题是videoPlayer的边界可能会改变。 因此,您需要将以下代码添加到视图控制器中,以相应地更改AVPlayerLayer的帧:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    playerLayer?.frame = view.bounds
}

希望有帮助。