ios – 设置AVSynchonizedLayer

前端之家收集整理的这篇文章主要介绍了ios – 设置AVSynchonizedLayer前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在尝试制作一个将视频与动画对象连接在一起的概念验证(实质上,我将用户选择的位图“旋转”到特定位置的罐头视频).在观看有关核心动画(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,1024,768);
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

CALayer *gerald = [CALayer layer];
gerald.frame = CGRectMake(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,[NSValue valueWithCGPoint:CGPointMake(480.0,206.0)],[NSValue valueWithCGPoint:CGPointMake(10.0,760.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;你会发现动画从视频开始播放时开始.

原文链接:https://www.f2er.com/iOS/335937.html

猜你在找的iOS相关文章