views:

3414

answers:

6

I want to show a MPMoviePlayerController in a view controller and let the user toggle full screen with the default controls, like the YouTube app. I'm using the following code in a bare-bones example:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.player = [[MPMoviePlayerController alloc] init];
    self.player.contentURL = theURL;
    self.player.view.frame = self.viewForMovie.bounds;
    self.player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.viewForMovie addSubview:player.view];
    [self.player play];
}

This works well until the user makes the video full screen, rotates the device and taps on the screen. The status bar is shown in the wrong position, as shown in the screenshot below.

http://yfrog.com/5fimg0006ryp

I'm working with the template Tab Bar Application for iPad. I've only added the viewDidLoad above, the view variables and an UIView in the XIB to show the movie player.

What am I doing wrong?

+2  A: 

Is shouldAutorotateToInterfaceOrientation:interfaceOrientation returning YES for all of the supported orientations?

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

If you provided more of your code it would help.

smountcastle
Yes. The only code that I've added to the project is the viewDidLoad above and of course the viewForMovie in the XIB. It's the default Tab Bar Application for iPad.
hgpc
+1  A: 

are you using interface builder for your UI? if so make sure you set the view's orientation to 'landscape' in the view attributes inspector.

corneliusk
It now fails in the other orientation.
hgpc
+1  A: 

I am experiencing the same issue. I have not been able to figure it out and I am have also used very barebones code.

Apple Developer forums proved helpless.

I really hope to find a solution soon.

Can you point to the related threads in the Apple Developer forums?
hgpc
Here the thread of the Apple developer forums. Please note that since I have posted the last comment, the workaround has stopped working and I am back at square one.https://devforums.apple.com/message/220995#220995
+1  A: 

Had the same problem, just spent half a day sorting it out. With the iPad in portrait orientation, whenever I started a video using the sample code (or any I could find on the net) the video and control bar were formatted for portrait, and hence all over the place on the screen.

Anyway, the following works for me.

  /* Call the code like below:
        int iLandscape;
        if( newOrientation==UIInterfaceOrientationLandscapeLeft || newOrientation==UIInterfaceOrientationLandscapeRight )
             iLandscape=1;

        [self PlayVideo:iLandscape fullscreen:1]
    */
        //////////////////////////////////////////////////////////////////////////
        - (void)PlayVideo:(int)iLandscape fullscreen:(int)iFullScreen 
        {
            NSString *url = [[NSBundle mainBundle] pathForResource:@"myvideofile" ofType:@"m4v"];

        if( iFullScreen==0 )
        {
            MPMoviePlayerController *player2 = 
                [[MPMoviePlayerController alloc] 
                    initWithContentURL:[NSURL fileURLWithPath:url]];

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

            //---play partial screen---
            player2.view.frame = CGRectMake(0, 0, m_iScreenWidth, m_iScreenHeight);
            [self addSubview:player2.view];
            //---play movie---
            [player2 play];
        }   
        else
        {
            MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc] 
                initWithContentURL:[NSURL fileURLWithPath:url]];

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

            if( iLandscape )
            {
                playerViewController.view.frame = CGRectMake(0, 0, m_iScreenWidth, m_iScreenHeight);
            }
            [self addSubview:playerViewController.view];
            //play movie
            MPMoviePlayerController *player = [playerViewController moviePlayer];
            player.scalingMode=MPMovieScalingModeAspectFit;
            [player play];
        }
    }


    //////////////////////////////////////////////////////////////////////////
    - (void) movieFinishedCallback:(NSNotification*) aNotification 
    {
        MPMoviePlayerController *player = [aNotification object];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];    
        [player autorelease];    
        [player.view removeFromSuperview];
    }
GMTDev
A: 

Did you solve this issue?

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

this code might help you.

SUKIYAKI
A: 

Yeah, I'm experiencing this problem as well. It definitely appears to be a bug in the MPMoviePlayerController itself.

The workaround I've settled on in my application is to just correct the status bar myself when I exit fullscreen mode:

- (void)playerDidExitFullscreen:(NSNotification *)notification {
    MPMoviePlayerController *moviePlayer = (MPMoviePlayerController *) notification.object;

    if (moviePlayer == self.player) {
        UIApplication *app = [UIApplication sharedApplication];
        if (app.statusBarOrientation != self.interfaceOrientation) {
            [app setStatusBarOrientation:self.interfaceOrientation animated:NO];
        }
    }
}

This doesn't fix the problem while in fullscreen mode, but it does fix it afterwards.

Note of course that the function needs to be added to the notification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerDidExitFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
Richard Venable