ios - 设置 AVSynchonizedLayer

标签 ios video animation core-animation sync

我一直在尝试制作一个将视频与动画对象连接起来的概念证明(本质上,我是在将用户选择的位图“rotoscoping”到特定位置的固定视频上)。在查看了有关 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 的正确格式或方式是什么,以便动画和视频一起播放?

最佳答案

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

Zero beginTime is automatically translated to CACurrentMediaTime(). Use a small nonzero number: e.g., 1e-100 or -1e-100 (AVCoreAnimationBeginTimeAtZero)

我认为如果您添加 anim.beginTime = AVCoreAnimationBeginTimeAtZero;,您会发现动画会在视频开始播放时立即开始。

关于ios - 设置 AVSynchonizedLayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5934662/

相关文章:

objective-c - 淡入淡出 UIView 和切换 UIView

jquery - 如何动画一个 div 打开其内容的大小

ios - 浅拷贝与深拷贝问题 iOS Swift

ios - 我如何读取 Apple 的崩溃日志

python - 如何确保使用OpenCV读取和处理所有帧

android - 如何在 android 中播放来自 URL 的流媒体视频?

javascript - 未捕获的类型错误 : Cannot read property 'getTotalLength' of null

ios - 将 Parse 注册和登录教程从 Obj-C 转换为 Swift

iphone - 性能不佳核心数据包含谓词

audio - 使用 FFmpeg 进行点对点流传输的问题