Question

i am trying to merge.mp4 and .caf file so i am using the following code,

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

but i am getting the error

*** 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


all require framework and .dylib file is included.

Was it helpful?

Solution

Print your array count. It must be an empty array that's why your app is getting crashed. Check that. NSlog(@"array count => %d",[videoAsset tracksWithMediaType:AVMediaTypeVideo]);

and replace your code by below code to prevent crashes.

 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]; 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top