How to play landscape video with MPMovieViewController in a portrait-only app
Asked Answered
A

1

6

My app's supported interface orientations are Portrait and Upside down. However, when a video is played, I want it to play full screen landscape. Currently with this code, it only plays portrait, even when the device is rotated:

[player.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
player.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self presentMoviePlayerViewControllerAnimated:player];

How can I force it into landscape mode?

Aker answered 22/10, 2012 at 23:35 Comment(1)
For iOS6, this answer is better : #12578379Informant
H
11

Here is how I do it:
In the project file, make sure you are supporting the Landscape Orientations supported interface orientations
Now in all of your ViewControllers that should still be Portrait Only, add this code

//ios6
- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

//ios4 and ios5
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

I have put in both the iOS5 and iOS6 calls so that my code will run on both.

When your MPMoviePlayerController view becomes fullscreen, it will be a new ViewController layered on top of everything else. So, it will be allowed to rotate according to the Supported Interface Orientations of the Project. It will not see where you forced the other ViewControllers into Portrait.

Hoarfrost answered 23/10, 2012 at 0:11 Comment(5)
thanks, I will resort to this if I have to, but I'm hoping there's a way to do it without having to put that orientation code in every view controller (there are a lot).Aker
What I do is create a UIViewController that just has these methods in it. Then I make all of my UIViewControllers that need to be portrait just inherit from this instead of inheriting from UIViewController. In the @interface line of the .h file I put the name of MyPortraitViewController in the place of UIViewController.Hoarfrost
@soleil, to my understanding, this is the only way to do it, unfortunately... the reason why is in the Apple docs on UIViewController under supportedInterfaceOrientations section: The system intersects the view controller’s supported orientations with the app's supported orientations (as determined by the Info.plist file or the app delegate's application:supportedInterfaceOrientationsForWindow: method) to determine whether to rotate.Skidmore
@Hoarfrost with first 3 methods included in superclass of all controllers (iOS6) it still auto-rotates in simulator. Am I missing something?Coniine
if you only support iOS 7, shouldAutorotate should do the jobSelectee

© 2022 - 2024 — McMap. All rights reserved.