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

标签 ios video swift3 autorotate

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

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 播放器上播放。 谢谢

最佳答案

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

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")
    }

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

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

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

关于ios - swift 3 - 旋转到横向时全屏播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42947215/

相关文章:

linux - 查找视频文件中的音频峰值

ios - 无法将 Swift CMTimeRange 转换为 Float 或获取数字?

ios - 更改 uiviewcontroller 时动画停止

ios - 如何使 UIScrollView 高度等于内容高度?

ios - tableView 项目上的 Thread1 :EXC_BAD_instruction(code=exc_i386_invop, 子代码=0*0)

video - 使用 HEVC HM 解码器引用 HEVC 文件解码

c# - 将 DirectShow 视频窗口附加到 WPF 控件

ios - 如何快速修复导航栏的透明度?

ios - 在更改 VC 之前获取并存储 PanoramaView 的位置

ios - 设计带有角边框的 View swift 3