使用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;你会发现动画从视频开始播放时开始.
总结以上是内存溢出为你收集整理的ios – 设置AVSynchonizedLayer全部内容,希望文章能够帮你解决ios – 设置AVSynchonizedLayer所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)