iphone - force MPMoviePlayerController to play video in landscape mode

15,134

Solution 1

Read this article:

http://iosdevelopertips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html

The main idea is:

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];

[[self view] setBounds:CGRectMake(0, 0, 480, 320)];
[[self view] setCenter:CGPointMake(160, 240)];
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];

Solution 2

My app is quite simple, with a UINavigationController as the root view controller which displays a main navigation screen, and a few detail screens. The main screen does not support rotation due to layout constraints, and for consistency the detail screens (which are simply pushed onto the navcontroller's stack) don't either. However, some of the detail screens contain links to videos, and the videos need to display in portrait mode. The app displays the player modally using MPMoviePlayerController.

I just had a similar issue, and tried the solution above which applies an affine transform to the MPMoviePlayerController's view, but the problem was that the status bar continued to display as in portrait mode, and overlapped the modal movie player view (on the left, if viewed in according to the rotation above). I tried a couple of things to no avail:

  • Hiding the status bar. This didn't work, because the player exists in its own world, and goes ahead and presents the status bar anyway. I couldn't find a way to make this go away.

  • Explicitly setting the status bar orientation. I'm not sure exactly why this didn't work, but I suspect that it was because I'd indicated in my info.plist that only portrait was supported and was therefore cut off from making changes.

Net-net, the above didn't work for me. Just as well, b/c Apple admonishes devs to treat the MPMoviePlayerController as opaque, and (especially with the transform method) I was violating this.

In the end, I found a simpler solution that worked for me:

  1. In the info.plist, I indicated that all orientations (except for upside down, so the standard iPhone idiom) were supported.

  2. Subclass UINavigationController, and override the appropriate shouldAutorotate methods so that only Portrait is supported (see this solution, among others, for how to do this in iOS <6 and iOS6 concurrently).

This worked because:

  1. While I'd indicated that autorotation is supported by the app, I cut it off at the UINavigationController subclass which contains all of the views I'm rendering... so everything remains portrait, except for:

  2. The MPMoviePlayerController is being presented modally, atop the NavigationController, and lives in its own world. Therefore it's free to pay attention to what's in the info.plist, and rotate all by itself.

There are a bunch of examples for how to present the player modally, but for quick reference, here's my code:

- (void)presentModalMediaPlayerForURL:(NSURL *)inURL
{
    NSLog(@"Will play URL [%@]", [inURL description]);
    MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:inURL];
    [player.view setBounds:self.view.bounds];

    [player.moviePlayer prepareToPlay];
    [player.moviePlayer setFullscreen:YES animated:YES];
    [player.moviePlayer setShouldAutoplay:YES];
    [player.moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
    [self presentModalViewController:player animated:YES];

    [player release];
}
Share:
15,134
sol
Author by

sol

Updated on June 04, 2022

Comments

  • sol
    sol almost 2 years

    I have an app that is portrait mode only, but when the user plays a video I want it to play in fullscreen landscape mode (the video player doesn't look good in portrait mode). I'm playing it like this:

    [self.view addSubview:myMoviePlayer.view];
    [self.myMoviePlayer setFullscreen:YES animated:YES];
    [self.myMoviePlayer play];
    

    What's the best way to accomplish this?

  • Tod Cunningham
    Tod Cunningham over 11 years
    This has been deprecated in IOS 6, and it wouldn't actually make the player rotate.
  • jesses.co.tt
    jesses.co.tt over 10 years
    this isn't working for me - i have a view controller which presents the movies, and its entirely in landscape mode, but the movie still launches in portrait... ideas ?