与AVMutableComposition视频创建问题

时间:2013-06-12 11:31:52

标签: iphone ios dylib avmutablecomposition

我正在尝试merge.mp4和.caf文件,所以我使用以下代码,

AVMutableCompositionTrack *compositionVideoTrack = 
[mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                            preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) 
                               ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                                                                 atTime:kCMTimeZero
                                                                  error:nil]; 

但我收到了错误

  

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'   *** First throw call stack:   (0x32c4d2a3 0x3aae397f 0x32b98b75 0x748e7 0x74e61 0x34b400c5 0x34b40077 0x34b40055 0x34b3f90b 0x34b3fe01 0x34a685f1 0x34a55801 0x34a5511b 0x367495a3 0x32c22683 0x32c21ee9 0x32c20cb7 0x32b93ebd 0x32b93d49 0x367482eb 0x34aa9301 0x6f767 0x6f708)   libc++abi.dylib: terminate called throwing an exception


所有需要框架并包含.dylib文件。

1 个答案:

答案 0 :(得分:2)

打印您的阵列计数。它必须是一个空数组,这就是你的应用程序崩溃的原因。检查一下。  NSlog(@"array count => %d",[videoAsset tracksWithMediaType:AVMediaTypeVideo]);

并使用以下代码替换您的代码,以防止崩溃。

 AVMutableCompositionTrack *compositionVideoTrack = 
    [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                preferredTrackID:kCMPersistentTrackID_Invalid];
    NSArray *dataSourceArray = [NSArray arrayWithArray: [videoAsset tracksWithMediaType:AVMediaTypeVideo];
   NSlog(@"array count => %d",[dataSourceArray count]);
    [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) 
                                   ofTrack:([dataSourceArray count]>0)?[dataSourceArray objectAtIndex:0]:nil
                                                                     atTime:kCMTimeZero
                                                                      error:nil];