Swift - MPMoviePlayerViewController 支持的设备方向

Swift - Supported Device Orientation for MPMoviePlayerViewController

好的,在我的应用程序上,当用户单击一个按钮时,一个视频从服务器流式传输并在 MPMoviePlayerViewController 中打开。

这是用户点击按钮时的代码:

@IBAction func WatchPressed(sender: AnyObject)
{self.presentMoviePlayerViewControllerAnimated(movieViewController)}

基本上我想知道我是否可以锁定放置按钮的主视图控制器的方向,然后在呈现 MPMoviePlayerViewController 时支持所有方向?如果可以,我可以使用标准支持的设备方向代码吗?

编辑:

使用此代码时:

    var movieplayer : MPMoviePlayerController!
    @IBAction func WatchPressed(sender: AnyObject)
    {self.presentMoviePlayerViewControllerAnimated(movieViewController)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "Rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)
    }
}


func Rotated()
{
    if UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation)
    {
        if UIDevice.currentDevice().orientation.rawValue == 4 ||  UIDevice.currentDevice().orientation.rawValue == 3
        {
            movieplayer.view.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)

        }
    }
    else if UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation)
    {
        if UIDevice.currentDevice().orientation.rawValue == 1 || UIDevice.currentDevice().orientation.rawValue == 2
        {
            movieplayer.view.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)

            let value = UIInterfaceOrientation.Portrait.rawValue
            UIDevice.currentDevice().setValue(value, forKey: "orientation")
        }

    }

我收到此错误:

首先在按钮中设置通知

override func viewWillAppear(animated: Bool) {
    let value = UIInterfaceOrientation.Portrait.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")
}

@IBAction func WatchPressed(sender: AnyObject)
{
 self.presentMoviePlayerViewControllerAnimated(movieViewController)
 NSNotificationCenter.defaultCenter().addObserver(self, selector: "Rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)
}

现在根据您的要求创建方法并设置方向

func Rotated()
{
if UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation)
    {
        if UIDevice.currentDevice().orientation.rawValue == 4 ||  UIDevice.currentDevice().orientation.rawValue == 3
        {
           movieViewController!.view.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)

        }
    }
    else if UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation)
    {
        if UIDevice.currentDevice().orientation.rawValue == 1 || UIDevice.currentDevice().orientation.rawValue == 2
        {
          movieViewController!.view.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)

            let value = UIInterfaceOrientation.Portrait.rawValue
            UIDevice.currentDevice().setValue(value, forKey: "orientation")
        }

    }
}

希望对你有所帮助!!!!!

你可以这样解决:

首先在你的 viewDidLoad 添加这个:

override func viewDidLoad() {

    //This observer will call doneButtonClick method when done button is pressed.
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "doneButtonClick:", name: MPMoviePlayerPlaybackDidFinishNotification, object: nil)

    var url = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!
    movieViewController = MPMoviePlayerViewController(contentURL: url)
}

之后添加这个方法:

func doneButtonClick(sender:NSNotification?){
    let value = UIInterfaceOrientation.Portrait.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")
}

这会强制将您的方向更改为纵向。

工作正常。

感谢@Dharmesh_Kheni