MPMoviePlayerController rotating in full screen while the parent View Controller only supports portrait orientation

11,832

Solution 1

you can try below function in AppDelegate:

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{

}

you can make condition here for both mode.

such as if media player is in full screen then

return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;

otherwise return UIInterfaceOrientationMaskPortrait;

i have not tried it but i think, it should work in your case.

thanks

Solution 2

For clarity, here is the complete code (it ALL goes inside your app delegate):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(willExitFullscreen:)
                                                 name:MPMoviePlayerWillExitFullscreenNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(willEnterFullscreen:)
                                             name:MPMoviePlayerWillEnterFullscreenNotification
                                           object:nil];
}

- (void)willEnterFullscreen:(NSNotification*)notification
{
    NSLog(@"willEnterFullscreen");
    isFullScreen = YES;
}

- (void)willExitFullscreen:(NSNotification*)notification
{
    NSLog(@"willExitFullscreen");
    isFullScreen = NO;
}

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (isFullScreen)
        return UIInterfaceOrientationMaskLandscapeLeft;
    else
        return UIInterfaceOrientationMaskPortrait;
}

isFullScreen is a BOOL to be declared in AppDelegate.h

Solution 3

This consumed me for a while and I got so many different horrifying errors, but eventually I ended up not doing it through MPMoviePlayerController but MPMoviePlayerViewController. I just rotated the self.playerView which is a property, before presenting it. Also I added the NSNotification that will lead back to the main control and the main ViewController after the video finishes. Here's how I went about executing it:

        [[NSNotificationCenter defaultCenter] removeObserver:self.playerView
                                                        name:MPMoviePlayerPlaybackDidFinishNotification
                                                      object:self.playerView.moviePlayer];

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(movieFinishedCallback:)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification
                                                   object:self.playerView.moviePlayer];

        self.playerView = [[MPMoviePlayerViewController alloc] initWithContentURL:docUrl];
        self.playerView.view.frame = CGRectMake(10, 10, self.frame.size.width-20, 180);
        [self.playerView.moviePlayer prepareToPlay];

        if(IS_IPHONE_6P)
        {
            [self.playerView.view setBounds:CGRectMake(0, 0, 736, 414)];
            [self.playerView.view setCenter:CGPointMake(212, 368)];
        }
        else if(IS_IPHONE_6)
        {
            [self.playerView.view setBounds:CGRectMake(0, 0, 375, 667)];
            [self.playerView.view setCenter:CGPointMake(187, 333)];
        }
        else if (IS_IPHONE_5)
        {
            [self.playerView.view setBounds:CGRectMake(0, 0, 736, 414)];
            [self.playerView.view setCenter:CGPointMake(160, 284)];
        }
        else
        {
            [self.playerView.view setBounds:CGRectMake(0, 0, 480, 320)];
            [self.playerView.view setCenter:CGPointMake(160, 240)];
        }

        [self.playerView.view setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
        self.playerView.modalPresentationStyle = UIModalPresentationFormSheet;
        self.playerView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

        [self presentViewController:self.playerView animated:YES completion:nil];

And the callback movieFinishedCallback: is as follows,

- (void)movieFinishedCallback:(NSNotification*)aNotification
{
    // Obtain the reason why the movie playback finished
    NSNumber *finishReason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];

    // Dismiss the view controller ONLY when the reason is not "playback ended"
    if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded)
    {
        MPMoviePlayerController *moviePlayer = [aNotification object];
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:MPMoviePlayerPlaybackDidFinishNotification
                                                      object:moviePlayer];

        NSLog(@"Video Closed");
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^
        {
            [self dismissViewControllerAnimated:NO completion:nil];
            self.playerView = nil;

        });
    }
}

This worked for me. Hope it helps.

Solution 4

I would suggest to use a MPMoviePlayerViewController instead. Subclass it and implement the supportedInterfaceOrientations method and return UIInterfaceOrientationMaskLandscape.

You might also have to implement the shouldAutorotateToInterfaceOrientation: method.

See the class reference: MPMoviePlayerViewController

Edit: You might also take a look at this post: iphone - force MPMoviePlayerController to play video in landscape mode

Share:
11,832
g.revolution
Author by

g.revolution

i m a geek :d SOreadytohelp

Updated on June 15, 2022

Comments

  • g.revolution
    g.revolution almost 2 years

    this question is only one part of my problem. I am implementing iOS6 rotation and orientation support for my existing application.

    So I have a ViewController that contains a MPMoviePlayerController embedded in ViewController view ( my application requires it ). User can play the video and see it in the embedded view or click on full screen button using the default player controls and player goes to full screen mode.

    Now I have restricted the view controller to only support portrait orientation using the new rotation APIs provided by iOS6.

    // New Autorotation support.
    - (BOOL)shouldAutorotate;
    {
        return NO;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait;
    }
    

    this works pretty well. the ViewController only supports portrait and user play the movie in embedded view.

    Now the problem comes, when User goes into full screen mode. In full screen mode, the movie is keep on rotating, when i rotate the simulator/device. When i rotate the device while movie being played in full screen mode with breakpoints in shouldAutorotate and supportedInterfaceOrientations , it still comes in these both methods which return NO and UIInterfaceOrientationMaskPortrait respectively, but still the movie is rotating ...

    Why is this happening? .... this is one part of my question ... the 2nd part is I want the movie to enter in landscape mode when the user goes to full-screen mode. and I want the movie player to lock in landscape mode until user presses the DONE button.

    Please help ....