【问题标题】:Transition between 2 movies using a MPMoviePlayerController使用 MPMoviePlayerController 在两部电影之间转换
【发布时间】:2012-04-21 22:06:47
【问题描述】:

我正在尝试在我的 iPad 应用程序中的 2 部电影之间进行平滑的淡入淡出。点击一个按钮,视觉效果随机化,正在播放的电影淡入下一部电影。

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


                                      }];

                 }]; 


}

【问题讨论】:

    标签: ios cocoa-touch mpmovieplayercontroller


    【解决方案1】:

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

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

    【讨论】:

    • 这似乎是解决问题的完美方法,但我无法将静止图像添加为电影的背景。行:[self.movi​​ePlayer.backgroundView addSubview:stillImageView];未实现 - 我所能看到的只是蓝色背景颜色(我为说明而设置的),仍然没有添加到背景颜色上。大型电影文件的处理是否可能由硬件编解码器处理,从而产生奇怪的行为?
    • 这很奇怪。我从我之前做过的一个项目中获取了我在这里发布的代码。我会尝试一些不同的变体,看看能否重现您的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    相关资源
    最近更新 更多