2012-03-30 75 views
7

我想使用AVAssetExportSession合併(追加)3視頻,但我不斷收到此錯誤。奇怪的是1或2個視頻的工作。iOS 5:與AVAssetExportSession合併3視頻錯誤

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

我甚至想重做功能在錯誤的情況下,但我得到的只有無限的錯誤消息。這是我的代碼片段。

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

我的代碼有什麼問題或iOS 5有一些錯誤?

回答

5

我發現了這個問題。所以問題實際上是因爲我使用AVPlayerLayer同時以預覽模式顯示每個視頻。參考AVPlayerItem fails with AVStatusFailed and error code "Cannot Decode"這個問題,有最多4個同時AVPlayer無法工作的限制。當這個時候有4個AVPlayer實例時,這個限制以某種方式妨礙了AVAssetExportSession的工作。

解決方法是在導出之前釋放AVPlayer,或者完全不使用AVPlayer。

+0

您還可以使用可變組合的副本初始化導出器 – Edwin 2014-10-02 02:55:07

+0

如何在目標c Xcode 8.2.1中釋放AVPlayer? – sohil 2017-10-11 19:31:24