AVSynchronizedLayer不同步动画

时间:2013-10-31 13:35:11

标签: ios iphone animation avfoundation

我遇到问题,使动画使用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];

1 个答案:

答案 0 :(得分:3)

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