【发布时间】: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