Han Pengbo
Han Pengbo

Reputation: 1436

MPMoviePlayerController.view isn't removed from superview

here is my code

- (void)viewDidLoad
{
...
 NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"mov"];
NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:_moviePlayerController.view];
_moviePlayerController.fullscreen = YES;
_moviePlayerController.scalingMode = MPMovieScalingModeAspectFit;
_moviePlayerController.controlStyle=MPMovieControlStyleDefault;
[_moviePlayerController play];

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieIsOver:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:nil];
}

- (void)movieIsOver:(NSNotification *)notification
{
    NSLog(@"movie is over");
   [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self.moviePlayerController.view removeFromSuperview];//moviePlayerController is MPMoviePlayerController
}

When the movie plays to the end, I can't see "movie is over" log and the moviePlayerController.view isn't removed. I don't know why.

EDIT: MPMoviePlayerPlaybackDidFinishNotification works well.I see the "movie is over" log.The problem is moviePlayerController.view isn't removed.

Upvotes: 0

Views: 905

Answers (2)

iBhavik
iBhavik

Reputation: 647

Instead of adding _moviePlayerController.view as subview

[self.view addSubview:_moviePlayerController.view];

you can present _moviePlayerController just like this :

[self presentMoviePlayerViewControllerAnimated: _moviePlayerController];

and in movieIsOver method you simply dismiss

[self dismissMoviePlayerViewControllerAnimated];

I hope its help

Upvotes: 0

Han Pengbo
Han Pengbo

Reputation: 1436

I found out the solution:add

_moviePlayerController.fullscreen = NO;

before removing view from superview

Upvotes: 1

Related Questions