Search code examples
iosxcodecocoampmovieplayercontrollermpmovieplayer

MPMoviePlayer not removing from superview


I am using the following code to play video files in MPMoviePlayerController

  NSString* filePath = [[NSBundle mainBundle] pathForResource:@"one" ofType:@"mp4"];
    NSURL*  url = [NSURL fileURLWithPath:filePath];

    _movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [_movie.view setFrame:self.view.bounds];
    [self.view addSubview:_movie.view];
    _movie.fullscreen=YES;
    _movie.controlStyle=MPMovieControlStyleFullscreen;
    [_movie prepareToPlay];
    [_movie play];

and

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(close:)name:MPMoviePlayerPlaybackDidFinishNotification object:_movie];

and

- (void) close:(NSNotification *)notification {

    int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];

    if(reason == MPMoviePlaybackStateStopped) {

        NSLog(@"Stop");

    }
    else if (reason == MPMovieFinishReasonPlaybackEnded) {
        NSLog(@"Playback Ended ");
    }
    else if (reason == MPMovieFinishReasonUserExited) {

        NSLog(@"Exited");
        [_movie.view removeFromSuperview];
    }
    else if (reason == MPMovieFinishReasonPlaybackError) {
        //error
        NSLog(@"Error");
    }    
}

I am able to get the Notification , and the Movieplayer is not removing from the superview.

What could be the problem ??


Solution

  • Try this follow instructions:

    when I listen to MPMoviePlayerWillExitFullscreenNotification.

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(doneButtonClick:) 
                                                 name:MPMoviePlayerWillExitFullscreenNotification 
                                               object:_movie];
    

    And selector method:

    -(void)doneButtonClick:(NSNotification*)aNotification{
      [_movie.view removeFromSuperview];
    }
    

    (or)

    Better way to use mpmovieplayerviewcontroller in below tutorial

    http://mobiledevelopertips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html