Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPMoviePlayerViewController becomes black when enters background

I have problem with MPMoviePlayerViewController , when app enters background and then I launch it again or go another viewControllers the movie became black ! I have movie which plays in the background of my menus , here is my code :

EIDTED CODE :

    -(void)viewDidLoad {
        [self moviePlayer2];
    } 

   - (void) moviePlayer2 {



    NSString *path = [[NSBundle mainBundle] pathForResource:@"cloud" ofType:@"mp4"];
    player = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
    player.view.userInteractionEnabled = YES;    
    player.moviePlayer.repeatMode = YES;
    player.moviePlayer.scalingMode = MPMovieScalingModeFill;
    player.moviePlayer.controlStyle = MPMovieControlStyleNone;

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(moviePlayBackStateChange:) 
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification
                                               object:[player moviePlayer]];

     [[player moviePlayer] play];

    [self.view addSubview:player.view]; 

}


-(void) moviePlayBackStateChange: (NSNotification *) note {

    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:[player moviePlayer]];

    [[player moviePlayer] play];

    //[player release];
    NSLog(@"FINISHED");
}

thank you .

like image 894
iOS.Lover Avatar asked Oct 10 '22 08:10

iOS.Lover


2 Answers

I think you may need to add codes below:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(moviePlayBackStateChange:) 
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification
                                               object:[player moviePlayer]];

and handle the movie state in the moviePlayBackStateChange method. The movie will be paused when the movie is playing and the app enters in background, so you need to make the movie resume like below when the app come back from background. If not,the movie will keep the pause state. That's why your app becomes black.

[[player moviePlayer] play];

then the movie will continue to play. adding two methods which you should invote when the app comes into background and backs from background:

-(void) pauseMovieInBackGround
{
   [player moviePlayer] pause];
   [player.view removeFromSuperview];
}
-(void) resumeMovieInFrontGround
{
   [self.view addSubview:player.view];
   [[player moviePlayer] play];
}

Hope this can help you guy.

like image 183
snail Avatar answered Oct 11 '22 20:10

snail


Try changing this:

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

To this:

[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:[player moviePlayer]];
[movieController.view removeFromSuperview];
[player release];

See if that works :D

like image 38
Matthew Hallatt Avatar answered Oct 11 '22 20:10

Matthew Hallatt