AVAsset" tracksWithMediaType"返回一个空数组 [英] AVAsset "tracksWithMediaType" returns an empty array

查看:1217
本文介绍了AVAsset" tracksWithMediaType"返回一个空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用来合并视频的方法的开始

These is the beginning o a method I am using to merge videos together

-(void) mergeVideosAndAudio:(AVAsset *)audioAsset{

    //Load Video Assets

    NSError *error;
    NSArray *dirFiles;
    if ((dirFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self documentsDirectory] error:&error]) == nil) {
        // handle the error
    };
    // find all the temp files
    NSArray *movFiles = [dirFiles filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self BEGINSWITH 'temp'"]];
    NSLog(@"The are %i temp files",movFiles.count);

    //Create assets array
    NSMutableArray *assets = [[NSMutableArray alloc]init];

    for (int i = 0; i < movFiles.count; i++) {
        NSString *videoURL = [[self documentsDirectory] stringByAppendingPathComponent:
                              [NSString stringWithFormat:@"temp%i.mov", i]];

        NSURL *url = [NSURL fileURLWithPath:videoURL];

        AVURLAsset *videoAsset = [AVURLAsset URLAssetWithURL:url options:nil];
        [assets addObject:videoAsset];

    }
        NSLog(@"assets:%i ", assets.count);
    // a second way
    for (id obj in assets)
        NSLog(@"obj: %@", obj);

    //Create the composition
    AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];

    // 1 - Video track
    AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                        preferredTrackID:kCMPersistentTrackID_Invalid];
    CMTime videoTrackDuration;
    for (int j = 0; j < assets.count; j++) {
        AVURLAsset *currentAsset = assets[j];
        videoTrackDuration = CMTimeAdd(videoTrackDuration, currentAsset.duration);
        CMTime time;
        if (j == 0) {
            time = kCMTimeZero;

        }else{
            AVURLAsset *previousAsset = assets[j-1];
            time = previousAsset.duration;
        }

        AVAssetTrack *assetTrack = [[currentAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

        [firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, currentAsset.duration) ofTrack:assetTrack atTime:time error:nil];
    }

我遇到的问题是 tracksWithMediaType currentAsset的属性是一个空数组。
这是控制台

The problem I am having is that the tracksWithMediaType property of currentAsset is an empty array. Here is the console

任何帮助将不胜感激。
谢谢

Any help will be greatly appreciated. Thanks

推荐答案

你见过这个链接

我正在研究解决方案KVO的轨道键:

I'm working on a solution now that KVO's the tracks key:

[item addObserver:self forKeyPath:kTracksKey options:opts context:nil];

这篇关于AVAsset&quot; tracksWithMediaType&quot;返回一个空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆