I am using a MPMoviePlayer to display a video. I go into full screen and when the done button is clicked I want it to remove of the entire movie player from my view. Currently it only goes out of the fullscreen mode. How do you track the doneButton being clicked or just how do I go about fixing this issue?
You can do that by adding a notification handler on MPMoviePlayerDidExitFullscreenNotification
as that notification gets sent once the user taps on the DONE Button.
Somewhere in your initializer
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerDidExitFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
Now implement that handler:
- (void)MPMoviePlayerDidExitFullscreen:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerDidExitFullscreenNotification
object:nil];
[moviePlayerController stop];
[moviePlayerController.view removeFromSuperview];
}
To the best of my knowledge, you can't be notified when the Done button is clicked. You can, however be notified when the movie player exits fullscreen after the Done button is clicked. For this, you use the MPMoviePlayerDidExitFullscreenNotification
To observe and act upon this notification you need to paste the following code in your class file which contains the IBAction (put it in the viewDidLoad
method):
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitedFullScreen) name:@"MPMoviePlayerDidExitFullscreenNotification" object:nil];
Now you need to create the exitedFullScreen
method in the same class:
-(void) exitedFullScreen
{
//Do whatever you want here
}
Finally, in your viewDidUnload
method, paste the following line:
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"MPMoviePlayerDidExitFullscreenNotification" object:nil];
To explain what's going on:
The "addObserver" line of code in your viewDidLoad makes sure your viewController responsible for handling the moviePlayer is listening to the MPMoviePlayerDidExitFullScreen notification.
That line makes it so that when the notification comes is, the exitedFullScreen method is fired off, where you would put the code you wanted to run when the Done button was clicked.
In the viewDidUnload, the viewController is going to be unloaded so you want to stop listening to the notification, hence the removeObserver part.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With