ios – AV基金会动画从未开始播放

ios – AV基金会动画从未开始播放,第1张

概述我试图加载一个视频,在它上面添加一个动画,然后导出它, 但动画永远不会在导出的视频中播放. 它只是显示图像“dogge_icon.png”. 我尝试过不同类型的动画,不知道我做错了什么. 任何帮助将非常感激. 代码: -(void) createCompositionWithPicture { AVMutableComposition* composition = [AVMutableCo 我试图加载一个视频,在它上面添加一个动画,然后导出它,
但动画永远不会在导出的视频中播放.
它只是显示图像“dogge_icon.png”.

我尝试过不同类型的动画,不知道我做错了什么.
任何帮助将非常感激.

代码:

-(voID) createCompositionWithPicture {    AVMutableComposition* composition = [AVMutableComposition composition];    Nsstring *vIDeoPath = [[NSBundle mainBundle] pathForResource:@"MovIE" ofType:@"m4v"];    NSLog(@"Path: %@",vIDeoPath);    NSURL *vIDeoURL = [[NSURL alloc] initfileURLWithPath:vIDeoPath];    AVURLAsset *vIDeoAsset = [AVURLAsset URLAssetWithURL:vIDeoURL options:nil];    AVMutableCompositionTrack *vIDeoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVIDeo preferredTrackID:kCMPersistentTrackID_InvalID];    [vIDeoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,vIDeoAsset.duration) ofTrack:[[vIDeoAsset tracksWithMediaType:AVMediaTypeVIDeo] objectAtIndex:0] atTime:kCMTimeZero error:nil];    // Create an AVMutableVIDeoCompositionLayerInstruction for the vIDeo track.    AVMutableVIDeoCompositionInstruction *mainInstruction = [AVMutableVIDeoCompositionInstruction vIDeoCompositionInstruction];    mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero,vIDeoAsset.duration);    AVMutableVIDeoCompositionLayerInstruction *vIDeolayerInstruction = [AVMutableVIDeoCompositionLayerInstruction vIDeoCompositionLayerInstructionWithAssetTrack:vIDeoTrack];    // Setup vIDeo composition    AVMutableVIDeoComposition *vIDeoComposition = [AVMutableVIDeoComposition vIDeoComposition];    vIDeoComposition.renderSize = CGSizeMake(vIDeoTrack.naturalSize.wIDth,vIDeoTrack.naturalSize.height);    vIDeoComposition.frameDuration = CMTimeMake(1,30);    mainInstruction.layerInstructions = [NSArray arrayWithObject:vIDeolayerInstruction];    vIDeoComposition.instructions = [NSArray arrayWithObject:mainInstruction];    NSLog(@"WIDth: %f Height: %f",vIDeoTrack.naturalSize.wIDth,vIDeoTrack.naturalSize.height);    // Setup animation layer    UIImage* image = [UIImage imagenamed:@"dogge_icon.png"];    CALayer *animationLayer = [CALayer layer];    animationLayer.frame = CGRectMake(0,image.size.wIDth,image.size.height);    [animationLayer setMasksToBounds:YES];    [animationLayer setContents: (ID)image.CGImage];    // Add animation    CABasicAnimation *animation =    [CABasicAnimation animationWithKeyPath:@"transform.scale"];    animation.duration=5.0;    animation.autoreverses=YES;    animation.fromValue = [NSNumber numberWithfloat:1.0f];    animation.tovalue = [NSNumber numberWithfloat:2.0f];    animation.repeatCount=10;    animation.beginTime = AVCoreAnimationBeginTimeatZero;    [animationLayer addAnimation:animation forKey:@"scale"];    NSLog(@"animationLayer animations: %@",[animationLayer animationKeys]);    // Build layer hIErarchy    CALayer *parentLayer = [CALayer layer];    CALayer *vIDeolayer = [CALayer layer];    parentLayer.frame = CGRectMake(0,vIDeoTrack.naturalSize.height);    vIDeolayer.frame = CGRectMake(0,vIDeoTrack.naturalSize.height);    [parentLayer addSublayer:vIDeolayer];    [parentLayer addSublayer:animationLayer];    vIDeoComposition.animationTool = [AVVIDeoCompositionCoreAnimationTool                             vIDeoCompositionCoreAnimationToolWithPostProcessingAsVIDeolayer:vIDeolayer inLayer:parentLayer];    // Export     AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetname:AVAssetExportPresetHighestQuality];    Nsstring *exportVIDeoPath =  [STPfileUtilitIEs getPathTofileIn: NSdocumentDirectory Withname: @"composition.mov"];    [STPfileUtilitIEs deletefileIfExists:exportVIDeoPath];    NSURL *exportURL = [NSURL fileURLWithPath:exportVIDeoPath];    exportSession.outputURL = exportURL;    exportSession.outputfileType = AVfileTypeQuickTimeMovIE;    exportSession.vIDeoComposition =  vIDeoComposition;    [exportSession exportAsynchronouslyWithCompletionHandler:^{        switch (exportSession.status) {            case AVAssetExportSessionStatusFailed:{                NSLog(@"FAIL: %@",exportSession.error);                break;            }            case AVAssetExportSessionStatusCompleted: {                NSLog (@"SUCCESS");            }        };    }];}
解决方法 我不确定我做了什么.
我从头开始重写了整个代码并添加了

animation.removedOnCompletion = NO;

所有动画,现在它的工作原理.

总结

以上是内存溢出为你收集整理的ios – AV基金会动画从未开始播放全部内容,希望文章能够帮你解决ios – AV基金会动画从未开始播放所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1041374.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存