当进入背景时,MPMoviePlayerViewController变黑
我有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"); }
谢谢 。
我想你可能需要在下面添加代码:
[[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]; }
希望这可以帮助你的家伙。
尝试改变这一点:
[[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