Abhijith
Abhijith

Reputation: 3404

MPMoviePlayerController cannot be removed using removeFromSuperView

I need to remove a MPMoviePlayerController from a View. I tried this.

[moviePlayerController stop];
[moviePlayerController.view removeFromSuperview];

the video stops, but the view is not removed. I guess [moviePlayerController.view removeFromSuperview]; does not work. What could be the reason ? Any Solution to this prolem ..?

Thanks.

Upvotes: 0

Views: 1451

Answers (6)

user4034301
user4034301

Reputation:

This problem generally occurs because of the player get deallocated.The solution is that declare the player instance in .h with property "strong".

@property (nonatomic,strong) MPMoviePlayerController* mpController;

Upvotes: 1

AlexanderN
AlexanderN

Reputation: 2670

Its a known problem that id you are using ARC, then you HAVE TO add the player to your .h because it does still get released if you declare it locally.

@property (nonatomic, strong) MPMoviePlayerController* controller;

To add the view:

self.controller = [[MPMoviePlayerController alloc] initWithContentURL:YOURVIDEOURL];

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

self.controller.controlStyle = MPMovieControlStyleDefault;
self.controller.shouldAutoplay = YES;

[self.view addSubview:self.controller.view];
[self.controller setFullscreen:YES animated:YES];

And then to remove the view:

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

if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{

    [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationPortrait animated:NO];

}

MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
 removeObserver:self
 name:MPMoviePlayerPlaybackDidFinishNotification
 object:player];

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

Upvotes: 1

Will
Will

Reputation: 1696

For me, I tried all of these: [moviePlayer stop]; [moviePlayer setContentURL:nil]; [moviePlayer.view removeFromSuperview]; moviePlayer = nil;

And nothing worked. I figured out it had to due with my MPMoviePlayerController entering full screen. The fix?

        [moviePlayer setFullscreen:NO animated:YES];

Upvotes: 0

DJean
DJean

Reputation: 334

[moviePlayerController stop];
[moviePlayerController setContentURL:nil];
[moviePlayerController.view removeFromSuperview];

this is running well in my project

Upvotes: 0

deleted_user
deleted_user

Reputation: 3805

Try dismissViewController:animated: That will probably work.

Upvotes: 0

Tom
Tom

Reputation: 346

Not entirely sure, but since I had the problem of the movieplayer not showing up because of auto deallocating, I guess you could just set moviePlayerController = nil;.

Not Totally sure if the view will disappear, but worth a try!

Upvotes: 0

Related Questions