2012-03-30 4 views
7

Sto tentando di unire (aggiungere) 3 video utilizzando AVAssetExportSession, ma continuo a ricevere questo errore. Stranamente per 1 o 2 video ha funzionato.iOS 5: errore di fusione di 3 video con AVAssetExportSession

Error Domain=AVFoundationErrorDomain Code=-11820 "Cannot Complete Export" UserInfo=0x458120 {NSLocalizedRecoverySuggestion=Try exporting again., NSLocalizedDescription=Cannot Complete Export} 

Ho anche provato a rifare la funzione in caso di errore, ma quello che ho ottenuto non è che il messaggio di errore infinita. Questo è lo snippet del mio codice.

AVMutableComposition *mixComposition = [AVMutableComposition composition]; 
AVMutableCompositionTrack *compositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 
NSError * error = nil; 
NSMutableArray * timeRanges = [NSMutableArray arrayWithCapacity:arrayMovieUrl.count]; 
NSMutableArray * tracks = [NSMutableArray arrayWithCapacity:arrayMovieUrl.count]; 

for (int i=0; i<[arrayMovieUrl count]; i++) { 
    AVURLAsset *assetClip = [arrayMovieUrl objectAtIndex:i]; 
    AVAssetTrack *clipVideoTrackB = [[assetClip tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 

    [timeRanges addObject:[NSValue valueWithCMTimeRange:CMTimeRangeMake(kCMTimeZero, assetClip.duration)]]; 
    [tracks addObject:clipVideoTrackB]; 
} 
[compositionTrack insertTimeRanges:timeRanges ofTracks:tracks atTime:kCMTimeZero error:&error]; 

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset1280x720]; 
NSParameterAssert(exporter != nil); 
exporter.outputFileType = AVFileTypeQuickTimeMovie; 
exporter.outputURL = outputUrl; 
[exporter exportAsynchronouslyWithCompletionHandler:^{ 
    switch ([exporter status]) { 
     case AVAssetExportSessionStatusFailed: 
      NSLog(@"Export failed: %@", [exporter error]); 
      break; 
     case AVAssetExportSessionStatusCancelled: 
      NSLog(@"Export canceled"); 
      break; 
     case AVAssetExportSessionStatusCompleted: 
      NSLog(@"Export successfully"); 
      break; 
     default: 
      break; 
    } 
    if (exporter.status != AVAssetExportSessionStatusCompleted){ 
     NSLog(@"Retry export"); 
     [self renderMovie]; 
    } 
}]; 

C'è qualcosa che non va nel mio codice o iOS 5 ha qualche bug?

risposta

5

Ho trovato il problema. Quindi il problema era in realtà perché uso AVPlayerLayer per visualizzare simultaneamente ogni video in modalità anteprima. Facendo riferimento a questa domanda AVPlayerItem fails with AVStatusFailed and error code "Cannot Decode", c'è un limite non documentato di massimo 4 AVPlayer simultaneo per funzionare. E questo limite in qualche modo impedisce ad AVAssetExportSession di funzionare quando ci sono 4 istanze di AVPlayer in quel momento.

La soluzione è rilasciare AVPlayer prima dell'esportazione o non utilizzare AVPlayer del tutto.

+0

è anche possibile inizializzare l'esportatore utilizzando una copia della composizione mutabile invece – Edwin

+0

Come rilasciare AVPlayer nell'obiettivo c Xcode 8.2.1? – sohil