设置AVSynchonizedLayer [英] Setting up AVSynchonizedLayer

查看:553
本文介绍了设置AVSynchonizedLayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图制造一个概念证明与动画对象(基本上,我在对位用户选择的位图到特定​​位置罐头视频)连接摄像。看着这一切关于核心动画(WWDC10等)和AVSync..Layer(苹果WWDC10样本code,文档样本$​​ C $ C)可用苹果材料之后,我们确实没有一个足够清晰的隔离code为例,得出如何正确地实施这种播放模式。

I've been trying to produce a proof of concept that connects a video with an animation object (essentially, I am "rotoscoping" a user-selected-bitmap onto a canned video at specific locations). After watching all the available Apple material regarding Core Animation (WWDC10, etc.) and the AVSync..Layer (Apple WWDC10 sample code, doc sample code), there simply isn't a clear enough isolated code example to derive how to implemented this playback mode correctly.

使用CA,我有被装入的CALayer视频资源,并且其连接到一个位图,然后连接到另一个的CALayer动画。我已经创建了一个AVSynchronizedLayer并添加两个对象的子层。

Using CA, I have a video asset which is loaded into a CALayer, and an animation which is attached to a bitmap, and then connected to another CALayer. I have created an AVSynchronizedLayer and added both objects as sublayers.

在此树被添加到了UIView的层属性我期待动画的播放,以与视频同步运行。相反,我只看到了视频播放和动画从未执行 - 在这里是我的code的相关作品,所有存在于单个视图控制器(称为内viewDidLoad中)

When this Tree is added to the UIView's layer property I was expecting the playback of the animation to run in sync with the video. Instead, I only see the video playing, and the animation never executes - here are the relevant pieces of my code which all resides within a single View Controller (called within ViewDidLoad).

Video101.m4v =简单M4V视频,
gerald.png =位图

Video101.m4v = simple m4v video, gerald.png = bitmap

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,这样既动画和视频播放在一起吗?

What is the correct format or manner to implement AVSynchronizedLayer, so that both animation and video playback together?

推荐答案

您有 CAKeyframeAnimation 设置正确,除了一个位:
按照WWDC与AV基金会编辑媒体的讲座,你需要动画的 BEGINTIME 属性设置为一个非零值。

You have the CAKeyframeAnimation set up correctly except for one bit: According to the WWDC "Editing Media with AV Foundation" lecture, you need to set the beginTime property of your animation to a non-zero value.

零BEGINTIME被自动转换为CACurrentMediaTime()。使用小型非零数字:例如,1E-100或-1e-100(AVCoreAnimationBeginTimeAtZero)

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

我觉得如果你添加 anim.beginTime = AVCoreAnimationBeginTimeAtZero; 你会发现在动画开始权当视频开始播放

I think if you add anim.beginTime = AVCoreAnimationBeginTimeAtZero; you'll find the animation begins right when the video starts playing.

这篇关于设置AVSynchonizedLayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆