swift 3 - 旋转为横向时全屏播放视频

swift 3 - play video in fullscreen when rotated to landscape

我有一个容器视图,可以在设备纵向时播放其中的视频。 我希望视频在设备旋转时自动全屏播放。 我已经尝试过这段代码来检测设备何时旋转但它不会将容器更改为全屏

override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {

    if (UIDevice.current.orientation.isLandscape) {

        videoContainerView.frame = self.view.frame
       // self.videoContainerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height) // this didn't work to....
        print("Device is landscape")
    }
    else{
        print("Device is portrait")
    }

}

该视频在 avplayer 播放器上播放。 谢谢

O.k。所以我设法通过将视频添加到 AVPlayerLayer 并将其大小设置为设备横向时的视图来全屏播放视频。

override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {

    if (UIDevice.current.orientation.isLandscape) {

                    DispatchQueue.main.async {
                       self.view.didAddSubview(self.controllsContainerView)
                       self.layer = AVPlayerLayer(player: self.player!)
                        self.layer?.frame = self.view.frame
                        self.layer?.videoGravity = AVLayerVideoGravityResizeAspectFill
                        self.view.layer.addSublayer(self.layer!)

        }
        print("Device is landscape")
    }

当 UIDevice.cuurent.orientation.islandscape == false 时,我删除 subLayer 并将视图设置回 videoContainer 边界。

else{
        print("Device is portrait")
        movie.view.frame = videoContainerView.bounds
        controllsContainerView.frame = videoContainerView.bounds
        self.layer?.removeFromSuperlayer()


    }

希望对大家有所帮助。

我找到了另一种更好的方法来解决这个问题,因为这样控件是可见的。主要思想是将 AVPlayerViewController 对象移动到一个新的子视图并将其框架设置为全屏。这是我的代码..

override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {

    if (UIDevice.current.orientation.isLandscape) {


                    DispatchQueue.main.async {
                        self.playerController.player = self.player
                        self.addChildViewController(self.playerController)
                        self.view.addSubview(self.playerController.view)
                        self.playerController.view.frame = self.view.frame

        }
        print("Device is landscape")
    }

当设备旋转回纵向时,将此子视图添加到上一帧并再次设置其大小。这是我的代码。

else{
        print("Device is portrait")
        videoContainerView.addSubview(playerController.view)
        playerController.view.frame = videoContainerView.bounds
        controllsContainerView.frame = videoContainerView.bounds
    }
}

希望对您有所帮助。祝你好运!

这对我有用,因为我使用的是 AVQueuePlayer:

override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
    super.willTransition(to: newCollection, with: coordinator)
    DispatchQueue.main.async {
        if (UIDevice.current.orientation.isLandscape) {
            self.landScapeAVController           = AVPlayerViewController()
            self.addChild(self.landScapeAVController!)
            self.landScapeAVController!.player   = self.queuePlayer
            self.landScapeAVController!.view.tag = 999
            self.view.addSubview(self.landScapeAVController!.view)
        } else {
            let topView                          = self.view.viewWithTag(999)
            topView?.removeFromSuperview()
            self.landScapeAVController?.removeFromParent()
        }
    }
}