设置AVSynchronizedLayer

时间:2011-05-09 08:58:19

标签: ios video animation core-animation sync

我一直在尝试制作一个将视频与动画对象连接起来的概念验证(实质上,我将用户选择的位图“旋转”到特定位置的固定视频上)。在观看了有关Core Animation(WWDC10等)和AVSync..Layer(Apple WWDC10示例代码,doc示例代码)的所有可用Apple资料后,根本没有足够明确的隔离代码示例来推导如何实现此正确播放模式。

使用CA,我有一个加载到CALayer中的视频资源,以及一个附加到位图的动画,然后连接到另一个CALayer。我创建了一个AVSynchronizedLayer并将这两个对象添加为子层。

当这个树被添加到UIView的图层属性时,我希望动画的播放能够与视频同步运行。相反,我只看到视频播放,动画永远不会执行 - 这里是我的代码的相关部分,它们都位于一个View Controller中(在ViewDidLoad中调用)。

Video101.m4v =简单的m4v视频, 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的正确格式或方式是什么,以便动画和视频一起播放?

1 个答案:

答案 0 :(得分:13)

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

  

Zero beginTime自动转换为CACurrentMediaTime()。使用小的非零数字:例如,1e-100或-1e-100(AVCoreAnimationBeginTimeAtZero)

我想如果你添加anim.beginTime = AVCoreAnimationBeginTimeAtZero;,你会发现动画在视频开始播放时就开始了。