进入后台时,MPMoviePlayerViewController变为黑色

Mc.*_*ver 5 iphone xcode mpmovieplayercontroller ipad ios

我有MPMoviePlayerViewController的问题,当应用程序进入后台然后我再次启动它或去另一个viewControllers电影变黑!我的电影在我的菜单背景中播放,这是我的代码:

EIDTED代码:

    -(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");
}
Run Code Online (Sandbox Code Playgroud)

谢谢 .

sna*_*ail 3

我认为您可能需要添加以下代码:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(moviePlayBackStateChange:) 
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification
                                               object:[player moviePlayer]];
Run Code Online (Sandbox Code Playgroud)

并在 moviePlayBackStateChange 方法中处理电影状态。当电影正在播放并且应用程序进入后台时,电影将暂停,因此当应用程序从后台返回时,您需要像下面一样使电影恢复。如果没有,电影将保持暂停状态。这就是你的应用程序变黑的原因。

[[player moviePlayer] play];
Run Code Online (Sandbox Code Playgroud)

然后电影将继续播放。添加当应用程序进入后台和从后台返回时应调用的两个方法:

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

希望这可以帮助你。