我正在尝试在我的 iPad 应用程序中的两部电影之间进行平滑的淡入淡出。在一个按钮上点击视觉随机化和正在播放的电影淡入下一部电影。
由于不能同时播放 2 部电影(来自文档),因此不可能直接进行交叉淡入淡出。为了解决这个问题,我使用了新电影初始帧的静止图像(要淡入)。我用两个播放器来处理电影的交换。
原始电影暂停,然后我过渡到图像。现在我添加我的第二部电影(alpha = 0.0)然后转换,使其 alpha = 1。然后我播放新电影。这一切正常,除了随着新电影在图像上的过渡而明显变暗。
我在没有静止图像的情况下对其进行了测试,并且在所述过渡期间似乎没有发生变暗。
我的开关代码如下(if 语句的前半部分)。看着它看起来很奇怪,但它是让我最接近我需要的方法。
也许有更好的方法来完成这项任务?提前致谢 :)
- (void) switchMovie;
{
NSURL *movieImageUrl = [movieImageUrlArray objectAtIndex:index];
NSData *data = [NSData dataWithContentsOfURL:movieImageUrl];
UIImage *movieImage = [[UIImage alloc] initWithData:data];
UIImageView *image = [[UIImageView alloc] initWithImage:movieImage];
image.alpha = 0.0f;
[self addSubview:image];
if (moviePlayer1Playing) {
moviePlayer1Playing = NO;
moviePlayer2Playing = NO;
[moviePlayer1 pause]; //pausing later in the method caused a crash, stopping causes a crash.
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationCurveEaseIn
animations:^{
image.alpha = 1.0f; //fade in still of first frame of new movie
} completion:^(BOOL finished) {
[moviePlayer1 release]; //release original movie.
moviePlayer2 = [[MPMoviePlayerController alloc] initWithContentURL:[movieUrlArray objectAtIndex:index]];
moviePlayer2.shouldAutoplay = NO;
[moviePlayer2 prepareToPlay];
[moviePlayer2 setControlStyle:MPMovieControlStyleNone];
moviePlayer2.scalingMode = MPMovieScalingModeAspectFill;
[moviePlayer2.view setFrame:CGRectMake(0, 0, 1024 , 768)];
moviePlayer2.view.backgroundColor = [UIColor clearColor];
moviePlayer2.view.alpha = 0.0;
[self addSubview:moviePlayer2.view];
[UIView animateWithDuration: 3.0
delay: 0.0
options: UIViewAnimationCurveLinear
animations:^(void){
moviePlayer2.view.alpha = 1.0f;
//During this transition the view goes dark half-way through, before lightening again.
} completion:^ (BOOL finished) {
[moviePlayer2 play];
moviePlayer2Playing = YES;
moviePlayer1Playing = NO;
}];
}];
}