【问题标题】:Adding a AVPlayerLayer as a sublayer of the current view does not work on an iPhone 5添加 AVPlayerLayer 作为当前视图的子层在 iPhone 5 上不起作用
【发布时间】:2015-06-26 05:49:56
【问题描述】:

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

这是我正在做的事情:

- (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 模拟器上会有一点延迟,显示黑色背景。

【问题讨论】:

  • 你有没有得到关于这个的明确答案?这也发生在我身上,而且非常令人抓狂。如果我弄清楚了,我一定会留下答案。
  • 是的,我刚刚发布了代码。我确保遵循@TheM00s3 所说的关于观察玩家状态的内容。
  • 啊,是的——这涉及到要点 2 和 3,但我刚刚学到了关于要点 1 的一些知识。你曾经能够让它在 iPhone 5 上播放吗?
  • @BenKreeger - 是的,我可以让它在 iPhone 5 上播放。

标签: ios objective-c iphone avplayer avplayerlayer


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我接受了@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"];
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多