Reputation: 542
I have a container view that plays the video in it when device is portrait. I want the video to play fullscreen automatically when device is rotated. I've tried this code that detects when device is rotated but it doesn't change the container to full screen
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")
}
}
The video is on an avplayer player. thank's
Upvotes: 1
Views: 8008
Reputation: 96
This did the trick for me, as I was using an 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()
}
}
}
Upvotes: 0
Reputation: 542
O.k. so i managed to play the video fullscreen by adding the video to an AVPlayerLayer and setting it's size to the view when Device is landscape.
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")
}
when UIDevice.cuurent.orientation.islandscape == false i remove subLayer and set the view back to videoContainer bounds.
else{
print("Device is portrait")
movie.view.frame = videoContainerView.bounds
controllsContainerView.frame = videoContainerView.bounds
self.layer?.removeFromSuperlayer()
}
hope it helps anyone.
Upvotes: 1
Reputation: 542
I found another and better way to solve this problem because on this way the controlls are visible. the main idea is to move the AVPlayerViewController object to A new subView and set It's frame to full screen. here is my code..
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")
}
When device is rotated back to portrait add this subView to the previous frame and set it's size again. here's my code.
else{
print("Device is portrait")
videoContainerView.addSubview(playerController.view)
playerController.view.frame = videoContainerView.bounds
controllsContainerView.frame = videoContainerView.bounds
}
}
Hope it helps. Good Luck!
Upvotes: 1