我有一个容器视图,当设备是纵向时,它会在其中播放视频。 我希望视频在旋转设备时自动播放全屏。 我已经尝试过这段代码,可以检测设备何时旋转,但不会将容器更改为全屏
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播放器上。 感谢的
答案 0 :(得分:1)
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()
}
希望它可以帮助任何人。
答案 1 :(得分:0)
我找到了解决这个问题的另一种更好的方法,因为这样控件是可见的。主要思想是将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
}
}
希望它有所帮助。祝你好运!
答案 2 :(得分:0)
这就像我使用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()
}
}
}