AVPlayer replaceCurrentItemWithPlayerItem打破了UIViewAnimation

时间:2012-07-31 16:07:04

标签: ios animation uiview avplayer

我有两个交叉淡入淡出的视图控制器,它们都在子视图中播放视频。视频A中的视频在淡入开始时开始播放,在淡出结束时停止播放。视图B中的视频以淡入开始,淡出后停止。基本的东西。 我用MPMoviePlayerController使它工作正常,但我也想做音频淡入/淡出,这迫使我从AVFoundationFramework转向AVPlayer。基本上一切似乎工作正常,但我注意到开关导致淡入打破。视图B alpha只是从0跳到1,UIView animateWithDuration的completition块被调用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];

    //....
}

有谁知道什么会导致动画破坏?我在aniation期间没有更改任何内容,因为在交叉淡入淡出开始之前调用loadView。什么可以导致相同的代码与MPMoviePlayerController一起使用并打破AVPlayer?

干杯, PB

1 个答案:

答案 0 :(得分:2)

事实证明,加载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]通知我的委托控制器它可以开始淡入。 太糟糕了,因为这个问题我的简单代码变得有点复杂。