Swift - Supported Device Orientation for MPMoviePlayerViewController

648 views Asked by At

Ok so on my app when the user clicks a button a video is streamed from a server and it opens in the MPMoviePlayerViewController.

This is the code for when the user clicks the button:

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

Basically I want to know whether or not I could lock the orientation of the main view controller that the button is placed on, and then support all orientations for when the MPMoviePlayerViewController is presented? If so could I use the standard supported device orientations code?

EDIT:

When using this code:

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

    }

I get this error: enter image description here

2

There are 2 answers

0
dwinnbrown On BEST ANSWER

You can solve it this way:

First of all In your viewDidLoad add this:

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

After that add this method:

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

Which will forcibly change your orientation to portrait.

It is working fine.

Credit to @Dharmesh_Kheni

1
Memon Irshad On

First set notification in button

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

Now create method and set orientation as per your requirement

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

    }
}

I hope it helpful for you!!!!!