iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – AV基金会动画从未开始播放大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图加载一个视频,在它上面添加一个动画,然后导出它,
但动画永远不会在导出的视频中播放.
它只是显示图像“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基金会动画从未开始播放所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。