So in my app there is an MPMoviePlayerViewController
which allows the user to view a video. They are able to watch the video in the MPMoviePlayerViewController
in any orientation and once they are finished and hit "done" they return to the previous view controller but in portrait only. The problem is that when the 'Done' button is pressed, the View Controller briefly flashes up in landscape before returning to portrait like it should. This is what it looks like when running: http://1drv.ms/1RSqUUD
My code is attached:
import UIKit
import MediaPlayer
class VideoViewController: UIViewController {
var movieViewController : MPMoviePlayerViewController?
var movieplayer : MPMoviePlayerController!
override func viewDidLoad() {
var url = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!
movieViewController = MPMoviePlayerViewController(contentURL: url)
}
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")
}
EDIT
When using this code: (as was suggested below) http://txt.do/nt1s The user is thrown out of the movie player when they rotate to landscape and they still briefly see the previous view controller in landscape before it rotates to portrait.
You can solve it this way:
First of all In your
viewDidLoad
add this:After that add this method:
Which will forcibly change your orientation to portrait.
It is working fine.