Dailycode.info

Short solution for short problems

MPMoviePlayerPlaybackDidFinishNotification results in a black screen

I had a hard time implementing the MPMoviePlayer. After a lot of search and some tutorials, I got what I wanted. But just one last thing was ginning me trouble. The user presses a button in the application to show the movie. Its shown in full screen mode, after the movie is finished the user should come back in the first screen. Here's the code on how I show the video (at the end I add the event DidFinish of the movie player to the notification center.):

 

- (IBAction)btnPlayVideo:(id)sender 
{
    MPMoviePlayerController *mov = [[MPMoviePlayerController allocinitWithContentURL:self.videoUrl];

    

    [mov prepareToPlay];
    mov.view.frame = self.view.bounds;
    mov.scalingMode = MPMovieScalingModeAspectFit;
    mov.movieSourceType = MPMovieSourceTypeFile;
    mov.controlStyle = MPMovieControlStyleDefault;
    self.moviePlayer = mov;
    [self.view addSubview:self.moviePlayer.view];
 
    self.moviePlayer.initialPlaybackTime = 0,0;
    [self.moviePlayer setFullscreen:YES animated:YES];
    [self.moviePlayer prepareToPlay];
    [self.moviePlayer play];

    

    [[NSNotificationCenter defaultCenteraddObserver:self selector:@selector(moviePlayBackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer ];
 
}

 

The problem was that when the movie is finished, I get a black screen and nothing is possible anymore in the application. The app is completely blocked. So I searched some and first somebody suggested to stop the movie before removing the player from the super view. But that didn't work. then another suggested to set the control style to none before removing it. Still nothing happened, but the last try was close to the solution I found there after. I had to set the control style to embedded before removing it. Here the code that worked for me at the end:

- (void) moviePlayBackComplete:(NSNotification *)notification

{

    MPMoviePlayerController *moviePlayer = [notification object];

    [[NSNotificationCenterdefaultCenter] removeObserver:selfname:MPMoviePlayerPlaybackDidFinishNotificationobject: moviePlayer];

   

    [moviePlayer setFullscreen: NO animatedYES];   

 

    [moviePlayer stop ];

    [moviePlayer setControlStyle:MPMovieControlStyleEmbedded];

    [moviePlayer.view removeFromSuperview];

}

 

It only worked every time after I added the line: setFullscreen: NO animated: YES. So the