How to force landscape mode in AVPlayer?

I am looking for a way to make AVplayer (Video) display only in landscape mode (the home button on the left). Is it possible?

EDIT: Trying to add playerViewController.supportedInterfaceOrientations = .landscapeLeft

Received error message "Property cannot be assigned:" supportedInterfaceOrientations "- get-only property"

import AVKit import AVFoundation UIViewController { var videoPlayer = AVPlayer() var playerViewController = AVPlayerViewController() override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) play() } func play() { playerViewController.supportedInterfaceOrientations = .landscapeLeft let moviePath = Bundle.main.path(forResource: "full video", ofType: "mov") if let path = moviePath { let url = NSURL.fileURL(withPath: path) let videoPlayer = AVPlayer(url: url) let playerViewController = AVPlayerViewController() playerViewController.player = videoPlayer self.present(playerViewController, animated: true) { if let validPlayer = playerViewController.player { validPlayer.play() playerViewController.showsPlaybackControls = false } } } } } 

EDIT: Attempting to add a new subclass of AVPlayerViewController

 import UIKit import AVFoundation import AVKit class LandscapeVideoControllerViewController: AVPlayerViewController { override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return .landscapeLeft } 

}

but receiving the error message "The method does not cancel any method from its superclass"

+6
source share
1 answer

If you use the AVPlayerViewController to represent the video, you should be able to use the inherited orientation property of the iterface orientation of the view controller. For instance:

 let vc = AVPlayerViewController() vc.supportedInterfaceOrientations = .landscapeRight // other configs and code to present VC 

I did not have the opportunity to verify this, but it should work, try, and if you encounter any problems, send the code that you have, and I will look and update the answer.

EDIT: Tyti noted that this property is read-only.

You should be able to implement it by extending the AVPlayerController and overriding the supported orientation properties. Create this class in a separate file:

 import AVKit class LandscapeAVPlayerController: AVPlayerViewController { override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return .landscapeLeft } } 

and then in your code above you change the class used as

 var playerViewController = LandscapeAVPlayerController() 

NOTE 1. In your code above, you create TWO instances of AVPlayerController, you do not need the second.

NOTE 2: Many of the configuration-related methods that you usually override in Swift are now properties in Swift 3, so instead of overriding the method, you override the "getter" for the property

+14
source

All Articles