进入后台时,MPMoviePlayerViewController变为黑色

时间:2011-12-01 16:35:46

标签: iphone ios xcode ipad mpmovieplayercontroller

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

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");
}
谢谢你。

2 个答案:

答案 0 :(得分:3)

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

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

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

[[player moviePlayer] play];
然后电影将继续播放。 添加两个方法,当应用程序进入后台并从后台返回时,您应该调用这些方法:

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

希望这可以帮助你。

答案 1 :(得分:1)

尝试更改此内容:

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

对此:

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

看看是否有效:D