Tuesday, July 12, 2011

Blank status bar problem with MediaPlayer


Playing videos in iOS is like butter and nothing much on developer side.

iOS has provided MediaPlayer.framework to play videos either from resource or from a http url.

In a full screen application development, you might come across the problem that, after finish of the video play the status bar is hidden and the place for status bar is left blank (white).

Also, this problem may occur when you present the MoviePlayerViewController on current view and not on adding MoviePlayerViewController's view on the parent view
i.e,
[self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
not on doing
[self.view addSubview:[[moviePlayerViewController moviePlayer] view]];

Hey, and do not forget to test your code on iPad device, as you can find this problem only on device & works fine on simulator.


You will notice that the status bar is left blank when you click on "Done" button while video is being played. If the video ends after full video play everything goes right.

This white status bar may look like this:



To handle this problem, We add observers for MPMoviePlayer class notifications and all you need is to set the status bar hidden in the method implementation of 'MPMoviePlayerPlaybackDidFinishNotification' notification, as below:

We can add MoviePlayer notification observers as below:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_MPMoviePlayerPlaybackDidFinishNotification) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

The implementation for 'MPMoviePlayerPlaybackDidFinishNotification' can be as below:

-(void)_MPMoviePlayerPlaybackDidFinishNotification{
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];

}

That is all we need to do.
You can download the working code here



No comments:

Post a Comment