使用MPMoviePlayerController在2部电影之间转换

时间:2012-04-21 22:06:47

标签: ios cocoa-touch mpmovieplayercontroller

我正试图在我的iPad App中的两部电影之间平滑交叉淡入淡出。在按钮上点击视觉随机化&正在播放的电影将逐渐淡出下一部电影。

由于无法同时播放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;


                                      }];

                 }]; 


}

1 个答案:

答案 0 :(得分:1)

你看到的黑色是电影播放器​​的背景,所以不要使用UIImageView作为静止图像,你应该将电影播放器​​的背景设置为所需的静止图像。

UIImageView *stillImageView = [UIImageView alloc]initWithImage:stillImage];
stillImageView.frame = self.moviePlayer.backgroundView.frame;

[self.moviePlayer.backgroundView addSubview:stillImageView];
[stillImageView release];