Hide StatusBar from MPMoviePlayerController
I've been struggling with a very annoying problem all day long and I hope I coul开发者_Python百科d find help on this board.
I'm using an MPMoviePlayerController to play a fullscreen movie on iPad and I can't figure how to remove the status bar which is always displayed despite all my efforts to make it go to hell.
Here is the code of the method I use to display the movie :
-(void)launchVideoFromButton:(id)sender{
NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"movie01" ofType:@"m4v"];
NSURL *videoPathURL = [NSURL fileURLWithPath:videoPath];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoPathURL];
[self.view addSubview:moviePlayer.view];
moviePlayer.shouldAutoplay = YES;
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[moviePlayer setFullscreen:YES animated:YES];
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(moviePlayerEvent:) name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayer];
}
-(void)moviePlayerEvent:(NSNotification*)aNotification{
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
NSLog(@"%i", [UIApplication sharedApplication].statusBarHidden);
}
In the console, I can see that moviePlayerEvent is fired when the movie appears but the statusbar is still there : [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO] seems to be inoperant. I've been trying to use the other MPMoviePlayerController notifications with no luck.
Could anyone help me on that one?
Thanks in advance.
Unfortunately, after running into this very problem, through research and much experimentation, I've determined that it is pretty much impossible to keep the iOS status bar hidden in full screen mode. No matter what you do, when the full screen player controls are shown, so will the status bar (it will not respect the setStatusBarHidden:YES
). This is not the case with the embedded player controls, but the user can easily switch between embedded and full screen modes, so you can't really use this to maintain no status bar when the controls are shown.
Of course, at least the status bar goes away when the controls fade out...
Do not add the movie player's view to your main view; instead, present the movie player modally as follows (some steps omitted for brevity/clarity):
moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
// Register for the playback finished notification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerViewController.moviePlayer];
//Present
[self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
// Play the movie!
self.moviePlayerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self.moviePlayerViewController.moviePlayer play];
// When the movie is done, release the controller.
-(void)myMovieFinishedCallback:(NSNotification*)aNotification
{
//NSLog(@"playback terminated");
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerViewController.moviePlayer];
[moviePlayerViewController release], moviePlayerViewController = nil;
}
It worked for me to use MPMoviePlayerViewController, setting up the following
[moviePlayerController.moviePlayer setFullscreen:YES animated:NO];
moviePlayerController.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
before this:
[self presentViewController:moviePlayerController animated:NO completion:^{ }];
and the following right after:
moviePlayerController.moviePlayer.controlStyle = MPMovieControlStyleNone;
just in case, I also did this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayerLoadStateDidChange:)
name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
...
- (void)moviePlayerLoadStateDidChange:(NSNotification *)notification {
if ([[moviePlayerController moviePlayer] loadState] == MPMovieLoadStateStalled) {
} else if([[moviePlayerController moviePlayer] loadState] != MPMovieLoadStateUnknown) {
[moviePlayerController moviePlayer].controlStyle = MPMovieControlStyleNone;
...
}
}
So, no status bars, no controls... nothing but only a pure video. )
(Tested on iOS 5.1 device and 6.0 simulator).
The status bar did hided, but showing up again with the play control.
-(void)viewDidLoad:{
[super viewDidLoad];
MPMoviePlayerViewController *moviePlayerViewController =
[[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playbackStateChange:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:moviePlayerViewController.moviePlayer];
}
-(void)playbackStateChange:(NSNotification*)notification{
if([[UIApplication sharedApplication]respondsToSelector:@selector(setStatusBarHidden: withAnimation:)])
[[UIApplication sharedApplication] setStatusBarHidden:YES
withAnimation:UIStatusBarAnimationNone];
else
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
}
Using MPMovieControlModeVolumeHidden didn't work for me, the only one that worked was MPMovieControlModeVolumeOnly with the video in fullscreen:
myMoviePlayer.controlStyle = MPMovieControlModeVolumeOnly;
[myMoviePlayer setFullscreen:YES];
And also, I am adding the movie view as a subview to the parent view:
[parentView addSubview:myMoviePlayer.view];
My app is supposed to not have status bar and for backwards compatibility I use the following code on the app delegate:
if([[UIApplication sharedApplication] respondsToSelector:@selector(setStatusBarHidden: withAnimation:)])
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
else
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
this is not an answer, I am having the same issue. There is one part I can update however..
The status bar only shows when the controls show.
Clicking on the movie, hides the conrols & the status bar, clicking again, shows the controls, and the status bar comes back too.
I am also hiding the status bar programmatically just before I launch the movie.
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
and here is how I am adding the movie:
[[[UIApplication sharedApplication] keyWindow] addSubview: movieView];
For those that have been running into this issue, I've found a solution of my own that might help. It only applies though if the rest of your app does not show the status bar and if you are trying to hide it again once the movie is done and returning to your interface, instead of during playback.
If your MPMoviePlayerController is being added as a subview to a UIView that is being pushed onto a navigation controller view stack, you can use that parent view controller's viewWillDisappear method to help you out.
In that method, you can change the control style to none, which will clear out any and all movie player controls before the view disappears and clears the status bar out as well if you already had it set to hidden. This will be completely unapparent to the user as the view is moving off screen and they are no longer interacting with it.
I had the same problem but I added to my info.plist
row Status bar is initially hidden - Boolean - YES
and it works!
BTW I'm using iOS 5.1, Xcode 4.3.2.
I don't know if my solution applies to your issue, but it works for my setup, i.e. 4th gen ipod running iOS 5.1.
My applications does not show the status bar at all, and in the info.plist file the corresponding entry "Status bar is initially hidden" is set to YES.
I also directly add the MPMoviePlayerController view to its parent view. Here is the code to setup the movie player:
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theurl];
[moviePlayer.view setFrame:frame]; // This is set to (0, 0, 320, 480)
[moviePlayer prepareToPlay];
[moviePlayer setShouldAutoplay:YES];
moviePlayer.fullscreen = TRUE;
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[self.view addSubview:moviePlayer.view];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playBackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
The moviePlayer is a class variable.
When the player finishes playing or when the viewer presses the "Done" button of the moviePlayer controller, the playbackFinished:
method is called:
- (void)playBackFinished:(NSNotification *)notif{
moviePlayer.controlStyle = MPMovieControlStyleNone;
[moviePlayer stop];
[moviePlayer.view removeFromSuperview];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[moviePlayer release];
moviePlayer = nil;
}
In which the control style of the moviePlayer is set to MPMovieControlStyleNone
to prevent any controls, but essentially the status bar from showing up when the moviePlayer is removed from its parent view.
精彩评论