Reputation: 149
As the question states, is there any way of rotating a video in avplayer.
Currently, I'm presenting a class that is an instance of avplayercontroller modally. The entire app runs only in portrait mode but I want this video to be played in landscape mode.
I've already tried the "shouldAutorotate" and other methods listed here: Only ONE VIEW landscape mode
Swift just doesn't seem to like allowing modally presented view controllers to be a different orientation.
Code for how my video controller class
import UIKit
import AVFoundation
import AVKit
class streamStuffViewController: AVPlayerViewController {
var isPresented = true
override func viewDidLoad() {
super.viewDidLoad()
self.player = AVPlayer(URL: NSURL(string: currentStream))
self.player.play()
// Do any additional setup after loading the view.
}
override func viewWillDisappear(animated: Bool) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidDisappear(animated: Bool) {
self.player.pause()
}
}
Upvotes: 1
Views: 4632
Reputation: 13557
I'm listening to orientation change:
// view did load
NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationDidChange), name: .UIDeviceOrientationDidChange, object: nil)
Then I rotate the video player but only to landscape left/right in my scenario:
@objc func deviceOrientationDidChange() {
switch UIDevice.current.orientation {
case .faceUp, .faceDown, .portrait, .unknown, .portraitUpsideDown:
// default the player to original rotation
self.playerController.view.transform = .identity
self.playerController.view.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
case .landscapeLeft:
self.playerController.view.transform = CGAffineTransform(rotationAngle: CGFloat((90 * Double.pi)/180))
self.playerController.view.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
case .landscapeRight:
self.playerController.view.transform = CGAffineTransform(rotationAngle: CGFloat((-90 * Double.pi)/180))
self.playerController.view.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
}
}
You can wrap that code inside UIView.animate
if you are into animations.
Don't forget to call NotificationCenter.default.removeObserver(self)
on deinit()
Upvotes: 0
Reputation: 41
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let navigationController = self.window?.rootViewController as? UINavigationController {
if navigationController.visibleViewController is PlayVideoViewController {
return UIInterfaceOrientationMask.all
} else {
return UIInterfaceOrientationMask.portrait
}
}
return UIInterfaceOrientationMask.portrait
}
If your application works only in potrait mode but you want just avplayer to rotate then add this code to AppDelegate.swift file and replace 'PlayVideoViewController' with your ControllerName.
Upvotes: 3
Reputation: 967
I too had the same situation just a while ago. I too tried to change the orientation only one screen and i did it with a hack i added
self.view.transform = CGAffineTransformMakeRotation(1.5708)
this line in viewDidLoad()
to rotate the whole view. But Still you will see the carrier and battery symbol in the portrait mode.
Upvotes: 0