Nassif
Nassif

Reputation: 1223

MPMovieplayerController goes black screen while rewind or forward is clicked

I am loading dynamic data, an array of dictionaries containing some description value and video URLs. The videos can be empty ,single or multiple URLs path.I am loading the video using the mpmovieplayercontroller ,while buffering if I click on the rewind or forward buttons, the screen goes black and I see no controls and I have to delete the app from the background. Is there a way to solve this.

NSURL *fileURL = [NSURL URLWithString:location];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];

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

self.moviePlayerController.controlStyle = MPMovieControlStyleFullscreen;
self.moviePlayerController.movieSourceType = MPMovieSourceTypeFile;
[self.moviePlayerController prepareToPlay];
[self.moviePlayerController.view setFrame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview:self.moviePlayerController.view];

Upvotes: 0

Views: 578

Answers (2)

matt
matt

Reputation: 534950

You are calling

[self.view addSubview:self.moviePlayerController.view];

every time without removing the movie player controller view you added the previous time. So your movie player controller views are just piling up on top of each other. Instead, use your existing reference to the old view to remove it:

NSURL *fileURL = [NSURL URLWithString:location];
if (self.moviePlayerController)
    [self.moviePlayerController.view removeFromSuperview];
self.moviePlayerController = 
    [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
// ... and so on ...

Another piece of advice: Don't actually call

[self.view addSubview:self.moviePlayerController.view];

until the movie is ready to play. Register for the notification that tells you this, and add the view to your interface when you get the notification:

__block id observer = [[NSNotificationCenter defaultCenter]
         addObserverForName:MPMoviePlayerReadyForDisplayDidChangeNotification 
         object:nil 
         queue:nil usingBlock:^(NSNotification *note) {
    if (self.moviePlayerController.readyForDisplay) {
        [[NSNotificationCenter defaultCenter] removeObserver:observer];
        [self.view addSubview:self.moviePlayerController.view];
    }
}];

Upvotes: 0

kamalesh kumar yadav
kamalesh kumar yadav

Reputation: 966

May this code help you try to use below code:

-(void) moviePlaybackComplete:(NSNotification*) aNotification 
{
    MPMoviePlayerController *moviePlayer = [aNotification object];

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

    [moviePlayer.view removeFromSuperview];

    [self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
}

may this help you and try to make changes app per your requirement

Upvotes: 2

Related Questions