0

I'm developing an iOS app and I want to play a video when the user pushes a button. I have developed this, the user pushes the button and the video is played, but when the video finishes, the view of the video remains, and it is frozen in the last frame of the video.

I have searching in Google, and I have found this question:

iOS 6, Xcode 4.5 video not exiting when done playing

I have used the code written there but I haven't fixed it. This is my code:

-(IBAction)reproducirVideo:(id)sender
{
NSURL *url5 = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                      pathForResource:@"instrucciones"     ofType:@"mp4"]];
_moviePlayer =  [[MPMoviePlayerController alloc]
                  initWithContentURL:url5];

_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES];
}

-(void) moviePlayBackDidFinish:(NSNotification *)aNotification{
[_moviePlayer.view removeFromSuperview];
_moviePlayer = nil;
}


- (void)moviePlayerWillExitFullscreen:(NSNotification*) aNotification {
[_moviePlayer stop];
[_moviePlayer.view removeFromSuperview];
_moviePlayer = nil;
}
Community
  • 1
  • 1
Luis RG
  • 13
  • 4

1 Answers1

1

Sorry for the inconvenience but I have read this question and I just fixed it:

MPMoviePlayerController will not automatically dismiss movie after finish playing (ios 6)

This is the right code:

- (IBAction)reproducirVideo:(id)sender
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                     pathForResource:@"instrucciones" ofType:@"mp4"]];
_moviePlayer =
[[MPMoviePlayerController alloc]
 initWithContentURL:url];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayBackDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:_moviePlayer];

_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES];
}

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

MPMoviePlayerController *player = [notification object];

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:MPMoviePlayerPlaybackDidFinishNotification
                                              object:player];

if ([player
     respondsToSelector:@selector(setFullscreen:animated:)])
{
    [player setFullscreen:NO animated:YES];
    [player.view removeFromSuperview];
}
}

THANKS JASEN!!! YOU SAVED MY LIFE

Best regards

Community
  • 1
  • 1
Luis RG
  • 13
  • 4