Swift - 支持MPMoviePlayerViewController的设备方向

时间:2015-06-05 12:00:19

标签: ios swift orientation mpmovieplayercontroller

好的,在用户点击按钮时我的应用程序视频从服务器流式传输并在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")
        }

    }

我收到此错误: enter image description here

2 个答案:

答案 0 :(得分:1)

首先在按钮

中设置通知
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)
}

enter image description here

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

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")
        }

    }
}

我希望它对你有帮助!!!!!

答案 1 :(得分:0)

你可以这样解决:

首先在你的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