3

I am using MoviePlayer controller to play a video in my iOS app. I am using orientation notification like this

if(deviceOrientation ==UIDeviceOrientationLandscapeLeft)

{
     NSLog(@"Replay is in Landscape");

     self.fullScreenFlag = YES;

     [self.moviePlayer setFullscreen:YES animated:NO];

}

This makes my video screen to play in full screen when user turns the phone to landscape orientation. But when I press done button on moviePlayer control I go into following method

- (void)movieWillExitFullscreen:(NSNotification*)notification
{

 UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;

if(deviceOrientation ==UIDeviceOrientationLandscapeLeft)  {
    NSLog(@"Pressed Done in Landscape");
    //Problem: Here I want to force my VideoViewController to rotate back to portrait  Mode
  }
}

Not sure how can I make the VC to go back to portrait as soon as user pressed done button or video stops playing. I am aware to the moviePlayerNotificationMethods but what should I call in those method for orientation is not clear.

Sam Spencer
  • 8,492
  • 12
  • 76
  • 133
CoffeeLife
  • 81
  • 5
  • 1
    MPMoviePlayerWhatever will rotate itself, regardless of what you do (other than setting the app's rotation attributes). So you need to make rotation correct for the rest of your code, ignoring movie player. – Hot Licks Jan 18 '13 at 01:12

3 Answers3

2

I solved this issue by having a separate view controller for the video playback.

So, you would have two view controllers

  • SomeViewController
  • MoviePlayerViewController

In your SomeViewController, when you want to play the movie:

MoviePlayerViewController *vc = [[MoviePlayerViewController alloc] initWithNibName:@"MoviePlayerViewController" bundle:nil];
[vc setPathToMovie:path];
[self.navigationController pushViewController:vc animated:YES];
[vc release];

And then in your MoviePlayerViewController

- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
    [[self navigationController] popViewControllerAnimated:YES];    
}

You can then lock down your SomeViewController to portrait, and if the user is in landscape when watching the video, they will return to portrait when popping back to SomeViewController.

I never found a solution using the deviceOrientation method and a modal MPMoviePlayerController. There may be one though!

James Zaghini
  • 3,895
  • 4
  • 45
  • 61
0

I solved this by doing this in "moviePlayBackDidFinish"

  UIViewController* forcePortrait = [[UIViewController alloc] init];
  [self presentViewController:forcePortrait animated:NO completion:^{
  [forcePortrait dismissViewControllerAnimated:NO completion:nil];
  }];

It's not beautiful but it works like a charm :-)

Jim Bamboo
  • 43
  • 4
0

Depending upon whether you using MPMoviePlayerController within a ViewController or as a separate ViewController the answer is as follows :-

Firstly :- This link will explain you how to restrict some views to portrait and allow others to rotate?

In that link you will see that, in the NavigationViewController you have made, following changes:-

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

What it does is, it give the child to make their own decision if they want to auto-rotate or not.

Next the ViewController containing your MoviePlayer should do this :-

-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

Once you have done this, it gives the power of AutoRotation to your ViewController.

Now here's the tricky part, see I assume that you might have restricted your ViewController to Portrait, and since movie player allows you go fullscreen and in fullscreen when you rotate your screen it will turn to landscape, and now if you press done button it won't turn to portrait rather it will exit the fullscreen in landscape itself. In this case what you should do is, in your:-

- (BOOL)shouldAutorotate
{

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    if (orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft) {
            if ([[[self.navigationViewController.viewControllers] lastObject] class] == [MoviePlayerViewController class] ) {
                return YES;
            }
            return NO;
        }

        return NO;
}

So, what it does is, you should auto-rotate only when the orientation is landscape and not when its portrait.

So far so good, next comes the MoviePlayer, considering that you have already played the Video and your only interest is when we click "Done" button it should auto-rotate to portrait.

Register for a notification to your MoviePlayer

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullScreen:) name:MPMoviePlayerWillExitFullscreenNotification object:_moviePlayer];

Then in the selector:

- (void) moviePlayerWillExitFullScreen:(NSNotification*)notification{

    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerWillExitFullscreenNotification object:_moviePlayer];
}

Tada! the magic is done! try out let me know ;-)

Community
  • 1
  • 1
Amit Singh
  • 1,075
  • 1
  • 7
  • 18