gpt4 book ai didi

ios - 设置 AVSynchonizedLayer

转载 作者:技术小花猫 更新时间:2023-10-29 10:35:48 26 4
gpt4 key购买 nike

我一直在尝试制作一个将视频与动画对象连接起来的概念证明(本质上,我是在将用户选择的位图“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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com