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?
2 Answers
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];
}

- 27,559
- 13
- 88
- 122
-
@Jackelope11 if this answer solved your question, please click on the checkmark to the left of this answer as well as on that arrow pointing up - thanks and I am glad this helped. – Till May 26 '11 at 19:58
-
I did the check but I don't have enough points to up the vote. I appreciated the answer though. – Jackelope11 May 26 '11 at 20:02
-
@Jackelope11 you checked the other answer :D - well, close enough - happy coding! – Till May 26 '11 at 20:24
-
@Hemang and how exactly can I be of help without you posting a question? – Till Apr 12 '12 at 13:45
-
I'm trying to figure this out myself--the problem with this solution is that you'll end up exiting if the user minimizes using the "shrink" button as well. This definitely doesn't seem like the right behavior. – Ryan Nov 29 '12 at 07:37
-
@Ryan the *DONE* button is for minimizing the player, not for finishing it. My solution is only useful for cases where you intend to allow fullscreen only. – Till Nov 29 '12 at 13:07
-
Fair enough, I was more focused on the "track the done button" being clicked part of your question, which is what I was trying to do. There's no easy, non-hacky way to differentiate between the player minimizing because of the "done" button vs. the minimize button, even though they do different things (done pauses and minimize doesn't) and say something different to the user ("I thought I said 'done,' why isn't it 'done'"? or, "I just wanted to minimize, why did it close!"). – Ryan Nov 29 '12 at 19:46
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.

- 9,508
- 5
- 39
- 60
-
thank you, I would like to know what using that would look like though. I start up the MPMoviePlayer with an IBAction so how would I implement your suggestion? – Jackelope11 May 26 '11 at 19:50
-
Sid, Is it really working? Because I trying your code in my app, but its not working as it should!!! Any help would be appreciated. – Hemang Apr 12 '12 at 10:34
-
Hemang, outside of syntax/spelling errors, I cannot know for sure why your code might not be working. Could you describe what's happening? – Sid Apr 12 '12 at 17:20
-
When my view loads, it started playing video in fullscreen mode, it has a button `DONE`, I want to exit from fullscreen when user presses it. – Hemang Apr 13 '12 at 04:05
-
Hemang, it's standard iOS behavior to automatically exit from fullscreen when the user presses Done. Do you mean you want to remove the subview? Seems like you should make this a question and list out exactly what youre doing, with screenshots if possible. – Sid Apr 13 '12 at 06:46
-
How can you distinguish between user tapping on done button vs user tapping on minimize button? In both cases the notification from the notification venter is the same but the end result is RADICALLY different...when Done button is tapped, move stops after being minimized where as in the other case it continues to play in mini zed state...I need to do something when Done is pressed... – Moonwalker Oct 30 '12 at 10:06
-
How about checking the 'playbackState' property when you receive the notification? – Sid Oct 30 '12 at 22:26