1

我有两个交叉淡入淡出的视图控制器,它们都在它们的子视图中播放视频。视图 A 中的视频在淡入开始时开始播放,在淡出结束时停止。视图 B 中的视频从淡入开始,在淡出后停止。基本的东西。我让它与 MPMoviePlayerController 一起工作得很好,但我也想做音频淡入/淡出,这迫使我使用 AVFoundationFramework 中的 AVPlayer。基本上一切似乎都很好,但我注意到开关导致淡入中断。视图 B alpha 只是从 0 跳到 1,并且 UIView animateWithDuration 的完成块被调用为 false 值。我注意到这只发生在我在 loadView 方法期间对 AVPlayer 对象调用 replaceCurrentItemWithPlayerItem: 方法时。

交叉渐变代码如下所示:

- (void)pushViewController:(UIViewController *)viewController duration:(float)duration
{    
    float halfFadeInOutTime = duration/2.0;
    viewController.view.alpha = 0.0;
    UIView *tmpView = self.view;


    [viewController viewWillAppear:YES];

    //start fading out the first view
    [UIView animateWithDuration:halfFadeInOutTime 
                          delay:0 
                        options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         tmpView.alpha = 0.0;
                     } 
                     completion:^(BOOL finished){
                         if( finished ){
                             if( [self respondsToSelector:@selector(fadeOutFinished:)] ){
                                 [self fadeOutFinished:duration];
                             }


                             [self.navigationController pushViewController:viewController animated:NO];

                             //start fading in with the second view
                             [UIView animateWithDuration:halfFadeInOutTime 
                                                   delay:0
                                                 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction
                                              animations:^{
                                                  viewController.view.alpha = 1.0;
                                              } 
                                              completion:^(BOOL finished2){
                                                  if( finished2 ){
                                                      if( [((CrossfadeableViewController*)viewController) respondsToSelector:@selector(fadeInFinished:)] ){
                                                          [((CrossfadeableViewController*)viewController) fadeInFinished:duration];
                                                      }
                                                  }
                                              }
                              ];

                             if( [((CrossfadeableViewController*)viewController) respondsToSelector:@selector(fadeInStarted:)] ){
                                 [((CrossfadeableViewController*)viewController) fadeInStarted:duration];
                             }


                         }
                     }
     ];

    if( [self respondsToSelector:@selector(fadeOutStarted:)] ){
        [self fadeOutStarted:duration];
    }
}

loadView 中的 AVPlayer 相关代码是这样的

- (void) loadView
{
    [super loadView]

    //...

    /* 
     * Variables of type:
     * UIView *videoPlaceholder;
     * AVURLAsset *asset;
     * AVPlayerItem *playerItem;
     * AVPlayer *player;
     * AVPlayerLayer *playerLayer;
     */ 

    videoPlaceholder = [[UIView alloc] initWithFrame:_frame];

    playerLayer = [[AVPlayerLayer alloc] init];
    [playerLayer setFrame:videoPlaceholder.frame];
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
    player = [[AVPlayer alloc] init];

    NSString *_url = [[NSBundle mainBundle] pathForResource:[_videoFilename stringByDeletingPathExtension] ofType:[_videoFilename pathExtension]];
    if( _url != nil ){
        NSURL *url = [NSURL fileURLWithPath:_url];

        asset = [AVURLAsset URLAssetWithURL:url options:nil];

        playerItem = [AVPlayerItem playerItemWithAsset:asset];

        [player replaceCurrentItemWithPlayerItem:playerItem];
        [player seekToTime:kCMTimeZero];
        [player setActionAtItemEnd:AVPlayerActionAtItemEndNone];
    }



    playerLayer.player = player;
    [videoPlaceholder.layer addSublayer:playerLayer];

    [self.view addSubview:videoPlaceholder];

    //....
}

有谁知道什么会导致动画中断?由于在交叉淡入淡出开始之前调用了 loadView ,因此在动画期间我没有更改视图中的任何内容。什么会导致相同的代码与 MPMoviePlayerController 一起工作并与 AVPlayer 中断?

干杯,铅

4

1 回答 1

3

事实证明这是加载 AVPlayerItem 的问题。我不得不等到电影加载完毕 - 否则动画淡入就坏了

等待视频完成加载的示例代码:

- (void)observeValueForKeyPath:(NSString*) path 
                  ofObject:(id)object 
                    change:(NSDictionary*)change 
                   context:(void*)context {
  if (context == AVPlayerDemoPlaybackViewControllerStatusObservationContext) {
      AVPlayerStatus status = [[change objectForKey:NSKeyValueChangeNewKey] integerValue];
      if (status == AVPlayerStatusReadyToPlay) {
        if( [self.videoStateChangeDelegate respondsToSelector:@selector(videoStateChangedToReadyForPlayback)] ){
            [self.videoStateChangeDelegate videoStateChangedToReadyForPlayback];
        }
        [self.player play];
      }
  }
}

[self.videoStateChangeDelegate videoStateChangedToReadyForPlayback] 通知我的委托控制器它可以开始淡入。太糟糕了,因为这个问题,我的简单代码变得有点复杂。

于 2012-08-15T09:56:48.093 回答