设置AVSynchonizedLayer

我一直在试图产生一个将video与animation对象连接起来的概念certificate(本质上,我将用户选定的位图“旋转”到特定位置的video上)。 看完有关Core Animation(WWDC10等)和AVSync..Layer(Apple WWDC10示例代码,doc示例代码)的所有可用Apple材料后,根本没有足够清晰的代码示例来推导如何实现播放模式正确。

使用CA,我有一个video资产被加载到一个CALayer,并附加到位图,然后连接到另一个CALayer的animation。 我创build了一个AVSynchronizedLayer并添加了两个对象作为子图层。

当这个树被添加到UIView的图层属性,我期待的animation播放与video同步运行。 相反,我只看到播放的video,animation从不执行 – 这里是我的代码的相关部分,它们都在一个View Controller中(在ViewDidLoad中调用)。

Video101.m4v =简单的m4vvideo,gerald.png =位图

NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"Video101" withExtension:@"m4v"]; AVURLAsset *asset = [AVURLAsset URLAssetWithURL:fileURL options:nil]; AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset]; AVPlayer *player = [[AVPlayer playerWithPlayerItem:playerItem]retain]; AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player]; playerLayer.frame = CGRectMake(0, 0, 1024, 768); playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; CALayer *gerald = [CALayer layer]; gerald.frame = CGRectMake(0, 0, 120, 120); gerald.contents = (id) [UIImage imageNamed:@"gerald.png"].CGImage; [self.view.layer addSublayer:playerLayer]; [self.view.layer insertSublayer:gerald above:playerLayer]; CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"]; anim.duration = 18.0; anim.values = [NSArray arrayWithObjects: [NSValue valueWithCGPoint:CGPointMake(320.0, 406.0)], [NSValue valueWithCGPoint:CGPointMake(310.0, 400.0)], [NSValue valueWithCGPoint:CGPointMake(320.0, 406.0)], [NSValue valueWithCGPoint:CGPointMake(310.0, 400.0)], [NSValue valueWithCGPoint:CGPointMake(320.0, 406.0)], [NSValue valueWithCGPoint:CGPointMake(310.0, 400.0)], [NSValue valueWithCGPoint:CGPointMake(480.0, 206.0)], [NSValue valueWithCGPoint:CGPointMake(310.0, 400.0)], [NSValue valueWithCGPoint:CGPointMake(320.0, 406.0)], [NSValue valueWithCGPoint:CGPointMake(320.0, 406.0)], [NSValue valueWithCGPoint:CGPointMake(310.0, 400.0)], [NSValue valueWithCGPoint:CGPointMake(320.0, 406.0)], [NSValue valueWithCGPoint:CGPointMake(10.0, 760.0)], [NSValue valueWithCGPoint:CGPointMake(320.0, 406.0)], [NSValue valueWithCGPoint:CGPointMake(310.0, 400.0)], [NSValue valueWithCGPoint:CGPointMake(320.0, 406.0)], [NSValue valueWithCGPoint:CGPointMake(310.0, 400.0)], nil]; anim.keyTimes = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.02], [NSNumber numberWithFloat:0.06], [NSNumber numberWithFloat:0.09], [NSNumber numberWithFloat:0.1], [NSNumber numberWithFloat:0.12], [NSNumber numberWithFloat:0.2], [NSNumber numberWithFloat:0.25], [NSNumber numberWithFloat:0.32], [NSNumber numberWithFloat:0.38], [NSNumber numberWithFloat:0.49], [NSNumber numberWithFloat:0.53], [NSNumber numberWithFloat:0.59], [NSNumber numberWithFloat:0.63], [NSNumber numberWithFloat:0.69], [NSNumber numberWithFloat:0.78], [NSNumber numberWithFloat:0.81], [NSNumber numberWithFloat:0.91], [NSNumber numberWithFloat:1.0], nil]; AVSynchronizedLayer *syncLayer = [AVSynchronizedLayer synchronizedLayerWithPlayerItem:playerItem]; [syncLayer addSublayer:gerald]; [gerald addAnimation:anim forKey:@"transform.position"]; [self.view.layer addSublayer:syncLayer]; [player play]; 

什么是实现AVSynchronizedLayer的正确格式或方式,使animation和video一起播放?

除了一个位之外,您还可以正确设置CAKeyframeAnimation :根据WWDC的“使用AV Foundation编辑媒体”讲座,您需要将animation的beginTime属性设置为非零值。

零beginTime被自动翻译成CACurrentMediaTime()。 使用一个小的非零数字:例如1e-100或-1e-100(AVCoreAnimationBeginTimeAtZero)

我想如果你添加anim.beginTime = AVCoreAnimationBeginTimeAtZero; 当video开始播放时,您会发现animation开始正确。