加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 进入后台时MPMoviePlayerViewController变黑

发布时间:2020-12-14 19:22:04 所属栏目:百科 来源:网络整理
导读:我有MPMoviePlayerViewController的问题,当应用程序进入后台然后我再次启动它或去另一个viewControllers电影变黑!我的电影在我的菜单背景中播放,这是我的代码: EIDTED代码: -(void)viewDidLoad { [self moviePlayer2]; } - (void) moviePlayer2 { NSStrin
我有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");
}

谢谢 .

解决方法

我想你可能需要在下面添加代码:

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

希望这可以帮助你.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读