AVPlayer图层不适合播放器视图

时间:2019-07-14 11:14:27

标签: ios swift avplayer ios-autolayout avplayerlayer

我已经实现了AV视频播放器视图,并且播放器层不适合较大屏幕的iPhone,例如6-8 plus和iPhone XS Max。

我的播放器视图为金色,这表示播放器视图的约束条件正在适应屏幕边缘,因此问题出在AVPlayerLayer。

请在下面检查我的代码,并根据我的代码库为我提供可用的代码段。

func setupVideoBg(){
        // Load the video from the app bundle.
        let videoURL: NSURL = Bundle.main.url(forResource: "video", withExtension: "mov")! as NSURL

        player = AVPlayer(url: videoURL as URL)
        player?.actionAtItemEnd = .none
        player?.isMuted = true

        let playerLayer = AVPlayerLayer(player: player)

       // playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
        playerLayer.videoGravity = AVLayerVideoGravity.resize
        playerLayer.zPosition = -1
    //    playerLayer.frame = CGRect(x: 0, y: 0, width: self.videoView.frame.size.width, height: self.videoView.frame.size.height)
        playerLayer.frame = videoView.layer.bounds
       // playerLayer.frame = videoView.bounds


        videoView.layer.addSublayer(playerLayer)
        playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
        player?.play()

        //loop video
        NotificationCenter.default.addObserver(self,
                                               selector: #selector
                                                (self.loopVideo),
                                               name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
                                            object: nil)
    }

    @objc func loopVideo() {
        player?.seek(to: CMTime.zero)
        player?.play()
    }

videoview in iphonexs max

1 个答案:

答案 0 :(得分:-1)

这是您可能遇到的问题,而没有看到如何设置videoView.layer:

1 playerLayer.zPosition = -1

(您可能有理由更改zPos,但是您可以简单地更改子图层的图层顺序)

2 playerLayer.frame = videoView.layer.bounds

((如果您的视图层不正确,则表示您很烦。您可以使用videoView.bounds.size.width的宽度并设置playerLayer.frame.size.width