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 :

  • ios overlaying alpha channel video on another video
  • Detecting when app is becoming active from lockscreen vs other on iOS7
  • Getting country calling prefix
  • Facebook Audience Network with Too many Warnings on XCode 7
  • UIWebview Zooming PDF and Large . Images Zoom By defaults IOS
  • UIScrollView's origin changes after popping back to the UIViewController
  •     -(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 .

    2 Solutions Collect From Internet About “MPMoviePlayerViewController becomes black when enters background”

    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.

    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 😀