I have a login screen that I wanted a video background for. It plays while the user taps the signup or login buttons. Standard thing you see in apps nowdays.
Here is what I am doing:
- (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];
}
It works great in the iOS Simulator on iPhone 5, but when testing it on a real device, the video never loads, I just get a black background. It also works correctly on my iPhone 6 (physical, not simulator).
This is a curious problem, which leads me to ask:
- Why would the video not load on iPhone 5?
- Should I be pre-loading the video somehow? (5.7MB .mp4)
- Is there a way to know when the video has been loaded and is ready to be displayed? Sometimes on the iPhone 5 simulator there is a bit of delay, which reveals the black background.