Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extremely weird behavior of navigationBar and MPMoviePlayerController. Bug in iOS or my error?

I have a MPMoviePlayerController object that plays a video fullscreen in either portrait or landscape. If I rotate orientation while the video is playing and do the rotation within a few seconds after the video starts playing and the video status bar is visible, when the video ends my navigation bar is perfect. But if I wait until the video status bar disappears a few seconds into the video playing and then rotate orientation, when the video ends my navigationBar is partially hidden behind the status bar, like pushed up.

Have you ever seen something like this?

I am able to recreate this bug easily. I created a new Single View App and simply added a button and a navigation bar. If I rotate orientation while video is playing, tap to enable fullscreen and the video status bar is still visible, when the video finishes, all is good. But, if I wait to rotate after the video status bar disappears, when I rotate and the video finishes, the navigationBar is under the status bar. See image:

iPhone Image

Here is the simple code I am using:

- (void) playMovie {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: @"movie" ofType: @"mov"]];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: url];

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

moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;

[self.view addSubview: moviePlayer.view];
[moviePlayer setFullscreen: YES animated: YES];

- (void) moviePlayBackDidFinish: (NSNotification *) notification
    MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver: self 
                                                name: MPMoviePlayerPlaybackDidFinishNotification 
                                              object: player];

if ([player respondsToSelector: @selector(setFullscreen:animated:)])
{
    [player.view removeFromSuperview];
}

Here is where I am currently at with the suggestions given below. I must have something wrong because unfortunately I still have the same problem.

Here is the method onPlayerWillExitFullScreen

UIView *view = [[[UIApplication sharedApplication] delegate].window.subviews lastObject];    
if (view) {
    [view removeFromSuperview];
    [[[UIApplication sharedApplication] delegate].window addSubview:view]; 
}

MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver: self 
                                                name: MPMoviePlayerWillExitFullscreenNotification 
                                              object: player];    

and here is my current playMovie method:

 NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: @"movie" ofType: @"mov"]];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: url];

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

[[NSNotificationCenter defaultCenter]addObserver: self
                                        selector: @selector(onPlayerWillExitFullScreen:) 
                                            name: MPMoviePlayerWillExitFullscreenNotification 
                                          object: self.moviePlayer];

moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;

[self.view addSubview: moviePlayer.view];
[moviePlayer setFullscreen: YES animated: YES];
like image 848
d.altman Avatar asked Dec 02 '11 05:12

d.altman


3 Answers

Ok, so I found this freaking same bug all over my app first in a UIWebView then in a MPMoviePlayerController, I solved this placing this code in my view controller.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

Tricky bugs, tricky fixes.

like image 181
3lvis Avatar answered Nov 04 '22 14:11

3lvis


If you listen for the MPMoviePlayerWillExitFullscreenNotification notification, you can force your main views to redraw properly as follows. The 'window' referenced is your application's main UIWindow object.

When MPMoviePlayerController switched to fullscreen, it actually creates a separate UIWindow instance to present the video. By catching the notification as it transitions back, this code will ensure the views you're switching back to correctly realign.

Admittedly, this is not an elegant solution, but it does work every time.

UIView *view = [window.subviews lastObject];
if (view) {
     [view removeFromSuperview];
     [window addSubview:view];
}

To listen for this notification, you'll need to do something like this, where self.playerController is your MPMoviePlayerController object.

Remember to stop listening for this notification once you release the player though!

    // Determine the default notification centre
    NSNotificationCenter *centre = [NSNotificationCenter defaultCenter];
    // Listen for interesting movie player notifications
    [centre addObserver: self
               selector: @selector(onPlayerWillExitFullScreen:) 
                   name: MPMoviePlayerWillExitFullscreenNotification 
                 object: self.playerController];
like image 5
reddersky Avatar answered Nov 04 '22 16:11

reddersky


- (void) moviePlayerWillExitFullScreen:(id)sender {

[[UIApplication sharedApplication]setStatusBarHidden:NO withAnimation:NO];

}

Guys try this... It works for me. I tried many other ways and only this one worked.

like image 3
Deepukjayan Avatar answered Nov 04 '22 15:11

Deepukjayan