1

我正在尝试合并 .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文件。

4

1 回答 1

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]; 
于 2013-06-12T12:03:42.913 回答