2014-10-22 13 views
7

Questi è l'inizio o un metodo che sto usando per unire video insiemeAVAsset "tracksWithMediaType" restituisce un array vuoto

-(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]; 
    } 

Il problema che sto avendo è che la proprietà di currentAsset tracksWithMediaType è un array vuoto. Ecco la console

enter image description here

Ogni aiuto sarà molto apprezzato. Grazie

+0

Si è verificato che currentAsset in realtà punta a un file vero e proprio sul file system? – jlw

+0

@jlw Come ho capito, currentAsset prende il file archiviato nella serie di risorse che può essere visto nella console.Right? – Kaitis

+0

Sì, ma la mia domanda era se tutti gli attuali Assets sono riproducibili tramite AVPlayer o MPMoviePlayer. È possibile creare un set di AVA con un NSURL che punta a una posizione che in realtà non contiene un file. – jlw

risposta

1

Avete visto this link?

sto lavorando su una soluzione, ora che KVO del tasto le tracce:

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