我在使动画使用 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];