3

我有一个登录屏幕,我想要一个视频背景。它在用户点击注册或登录按钮时播放。您现在在应用程序中看到的标准内容。

这是我正在做的事情:

- (void)addPlayerLayer {

    NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"login_signup_pink-girl" ofType:@"mp4"];
    NSURL *movieURL = [NSURL fileURLWithPath:moviePath];

    AVPlayer *player = [[AVPlayer alloc] initWithURL:movieURL];
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    playerLayer.frame = CGRectMake(0,0,self.view.frame.size.width+self.screenShift, self.view.frame.size.height);
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

    self.playerView = [[UIView alloc] initWithFrame:self.view.bounds];
    self.playerViewFrame = self.playerView.frame;
    [self.playerView.layer addSublayer:playerLayer];
    self.playerView.alpha = 0;
    [self.view insertSubview:self.playerView atIndex:0];
    [playerLayer.player play];

    [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.playerView.alpha = 1.0;
    } completion:nil];

    [UIView animateWithDuration:0.6 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{

        self.loginButton.alpha = 1.0;
        self.createAccountButton.alpha = 1.0;
        self.skipStepButton.alpha = 1.0;
        self.mainSTbackgroundImageView.alpha = 0.0;

    } completion:nil];

}

它在 iPhone 5 上的 iOS 模拟器中运行良好,但在真实设备上测试时,视频永远不会加载,我只是得到黑色背景。它也可以在我的 iPhone 6 上正常工作(物理,而不是模拟器)。

这是一个奇怪的问题,这让我问:

  • 为什么视频无法在 iPhone 5 上加载?
  • 我应该以某种方式预加载视频吗?(5.7MB .mp4)
  • 有没有办法知道视频何时已加载并准备好显示?有时在 iPhone 5 模拟器上会有一点延迟,显示黑色背景。
4

2 回答 2

1

仅仅因为您添加了一个播放器层,它就可以播放了。您想为播放器本身添加一个观察者,并观察它何时变为 ReadyToPlay 的状态。可能发生的情况是,在模拟器上您使用 Wi-Fi,而在电话上使用您的连接可能比使用 Wi-Fi 甚至固定电话要慢。

于 2015-11-04T20:02:07.657 回答
0

我接受了@TheM00s3 的建议并做了以下事情:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                        change:(NSDictionary *)change context:(void *)context {
    if (object == self.player && [keyPath isEqualToString:@"status"]) {
        if (self.player.status == AVPlayerStatusReadyToPlay) {

            // Fade in top logo
            [UIView animateWithDuration:0.4 delay:0.2 options:UIViewAnimationOptionCurveEaseInOut animations:^{

                self.mainSTLogoTop.alpha = 1.0;

            } completion:nil];

            // Fade in video and out logo and text
            [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{

                self.mainSTLogo.alpha = 0.0;
                self.animatedSequence.alpha = 0.0;

            } completion:^(BOOL finished) {

                // Add the video player
                [self.player play];

            }];

            [self.player removeObserver:self forKeyPath:@"status"];

        } else if (self.player.status == AVPlayerStatusFailed) {
            // something went wrong. player.error should contain some information
            NSLog(@"Error: %@", self.player.error);
            [self.player removeObserver:self forKeyPath:@"status"];
        }
    }
}
于 2016-04-13T20:34:52.420 回答