3

我在使动画使用 AVPlayer 时间而不是系统时间时遇到问题。同步层无法正常工作,动画在系统时间而不是播放器时间上保持同步。我知道玩家确实在玩。如果我将 CACurrentMediaTime() 传递给 beginTime,动画会立即开始,因为它应该在不同步时开始。

编辑

我可以看到从一开始就处于最终状态的红色方块,这意味着动画在开始时已经结束,因为它是在系统时间而不是 AVPlayerItem 时间同步的。

    // play composition live in order to modifier
    AVPlayerItem  * playerItem  = [AVPlayerItem  playerItemWithAsset:composition];
    AVPlayer      * player      = [AVPlayer      playerWithPlayerItem:playerItem];
    AVPlayerLayer * playerLayer = [AVPlayerLayer playerLayerWithPlayer:player   ];
    playerLayer.frame           = [UIScreen mainScreen].bounds;

    if (!playerItem) {
        NSLog(@"playerItem empty");
    }
    // dummy time
    playerItem.forwardPlaybackEndTime = totalDuration;
    playerItem.videoComposition       = videoComposition;

    CALayer * aLayer       = [CALayer layer];
    aLayer.frame           = CGRectMake(100, 100, 60, 60);
    aLayer.backgroundColor = [UIColor redColor].CGColor;
    aLayer.opacity         = 0.f;

    CAKeyframeAnimation * keyframeAnimation2 = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
    keyframeAnimation2.removedOnCompletion   = NO;
    keyframeAnimation2.beginTime             = 0.1;
    keyframeAnimation2.duration              = 4.0;
    keyframeAnimation2.fillMode              = kCAFillModeBoth;
    keyframeAnimation2.keyTimes              = @[@0.0, @1.0];
    keyframeAnimation2.values                = @[@0.f, @1.f];
    NSLog(@"%f current media time", CACurrentMediaTime());

    [aLayer addAnimation:keyframeAnimation2
                  forKey:@"opacity"];

    [self.parentLayer addSublayer:aLayer];

    AVSynchronizedLayer * synchronizedLayer =
    [AVSynchronizedLayer synchronizedLayerWithPlayerItem:playerItem];


    synchronizedLayer.frame = [UIScreen mainScreen].bounds;
    [synchronizedLayer addSublayer:self.parentLayer];
    [playerLayer       addSublayer:synchronizedLayer];
4

1 回答 1

3

解决方案是AVSynchronizedLayer在模拟器上不起作用,但在设备上可以正常工作。

于 2013-11-15T09:16:57.663 回答