How to force landscape mode in AVPlayer?

大城市里の小女人 提交于 2019-12-12 07:59:25

问题


I'm looking for a way to force a AVplayer (Video) to only display itself in landscape mode (home button on the left). Is this possible?

EDIT: Attempted to add playerViewController.supportedInterfaceOrientations = .landscapeLeft

Got error message "Cannot assign to property: 'supportedInterfaceOrientations' is a 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: Attempted to add new subclass of AVPlayerViewController

import UIKit
import AVFoundation
import AVKit

class LandscapeVideoControllerViewController: AVPlayerViewController {

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return .landscapeLeft
}

}

but getting error message "Method does not override any method from its superclass"


回答1:


If you are using the AVPlayerViewController to present the video you should be able to use the inherited supported iterface orientations property of the view controller, Like so:

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

I haven't had chance to test this but it should work, give it a try and if your having any issues please post the code you have and I'll have a look and update the answer.

EDIT: Taytee pointed out that this is a read only property.

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

import AVKit

class LandscapeAVPlayerController: AVPlayerViewController {

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscapeLeft
    }
}

and then in your code above, you'd change the class used like

var playerViewController = LandscapeAVPlayerController()

NOTE 1: In your code above you are creating TWO instances of AVPlayerController, you dont need the second one.

NOTE 2: Alot of the config related methods you would normally override in Swift are now properties in Swift 3, so instead of overriding the method, you override the 'getter' for the property




回答2:


Just create sub-class of AVPlayerViewController

import AVKit

class LandscapePlayer: AVPlayerViewController {
      override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
          return .landscapeLeft
       }
}

Just as below.

let player = AVPlayer(url:Video_URL)
let playerController = LandscapePlayer()
playerController.player = player
present(playerController, animated: true) {
   player.play()
}


来源:https://stackoverflow.com/questions/39519938/how-to-force-landscape-mode-in-avplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!