进入后台时,MPMoviePlayerViewController变为黑色 [英] MPMoviePlayerViewController becomes black when enters background

查看:92
本文介绍了进入后台时,MPMoviePlayerViewController变为黑色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

谢谢。

推荐答案

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

I think you may need to add codes below:

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

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

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];
}

希望这可以帮到你。

这篇关于进入后台时,MPMoviePlayerViewController变为黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆