问题
My app has to be work in Ios5 and later Versions. I am adding MPMoviePlayerViewController to mainWindow When clicking on a button.the Done button of moviePlayerController is not removing the moviePlayerController from window.
My code is
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"001 ATSW" ofType:@"m4v"]];
self.player = [[MPMoviePlayerViewController alloc]
initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self.player.view setFrame:[UIScreen mainScreen].bounds];
[[[UIApplication sharedApplication] keyWindow] addSubview:self.player.view];
[[self.player moviePlayer] play];
}
-(void)videoPlayBackDidFinish:(NSNotification*)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self.player.moviePlayer stop];
self.player = nil;
[self.player.view removeFromSuperview];
}
How to remove the moviePlayerController on clicking the Done Button.
Any help please.
回答1:
As stated in the documentation:
To dismiss a modally presented movie player view controller, call the
dismissMoviePlayerViewControllerAnimated
method.
As with other preconfigured controllers by Apple (such as the mail compose controller or image picker controller), you are responsible to dismiss the controller.
回答2:
What you'll probably need to do is to actually using the
presentMoviePlayerViewControllerAnimated:(MPMoviePlayerViewController *)playerVC
method to present the MPMoviewPlayerViewController, and not inserting the view directly. As MPMoviewPlayerViewController is trying to dismiss itself when pressing the done button. And from what I can see there's no good way to listen for that and do it like when the movie finishes.
It might depend on how your app is built, but with something like
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentMoviePlayerViewControllerAnimated:player];
You should be able to replace all of the code to insert a view maualy, and then just using
[[[[UIApplication sharedApplication] keyWindow] rootViewController] dismissMoviePlayerViewControllerAnimated];
来源:https://stackoverflow.com/questions/14773338/mpmovieplayerviewcontroller-done-button-is-not-removing-the-player