添加 AVPlayerLayer 作为当前视图的子层在 iPhone 上不起作用 5

Adding a AVPlayerLayer as a sublayer of the current view does not work on an iPhone 5

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

这是我正在做的事情:

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

}

它在 iOS Simulator on iPhone 5 上运行良好,但在真实设备上测试时,视频永远不会加载,我只会看到黑色背景。它也适用于我的 iPhone 6(物理,而非模拟器)。

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

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

我听取了@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"];
        }
    }
}