My brain feels like scrambled eggs... I'm trying to merge video clips together. I have each clip URL stored in an NSMutableArray (arrayClipURL) & that's all good. When I print timeRanges & tracks (both NSMutableArrays) in debug console, everything checks out, which means for loop is doing its job. I keep getting an exception error: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'.
I threw in an exception breakpoint and its breaking at the last line below. I cant figure it out because both timeRanges and tracks are NOT nil... I can print them in the debug console and see them just fine directly before the line that breaks.
composition = [[AVMutableComposition composition] init];
composedTrack = [[AVMutableCompositionTrack alloc] init];
composedTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo
preferredTrackID:kCMPersistentTrackID_Invalid];
NSMutableArray * timeRanges = [[NSMutableArray alloc] initWithCapacity:arrayClipURL.count];
NSMutableArray * tracks = [[NSMutableArray alloc] initWithCapacity:arrayClipURL.count];
for (int i=0; i<[arrayClipURL count]; i++){
AVURLAsset *assetClip = [[AVURLAsset alloc] initWithURL:[arrayClipURL objectAtIndex:i] options:nil];
AVAssetTrack *clipTrack = [[AVAssetTrack alloc] init];
clipTrack = [[assetClip tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
[timeRanges addObject:[NSValue valueWithCMTimeRange:CMTimeRangeMake(kCMTimeZero, assetClip.duration)]];
[tracks addObject:clipTrack];
}
debug(@"timeranges: %@",timeRanges);
debug(@"tracks: %@",tracks);
[composedTrack insertTimeRanges:timeRanges ofTracks:tracks atTime:kCMTimeZero error:nil];
I am in need of some veteran assistance :( Any idea what could be causing the problem?
Edit: Here are how the 2 arrays look printed in the console. The only thing I can think of is maybe the CMTimeRange or AVAssetTrack aren;t formatted properly in the arrays??? i have no idea. Just thought it might help to see what it's actually trying to insert when the exception is thrown.
2013-02-18 13:18:20.811 app[5242:907] [AVCaptureManager.m:401] timeranges array: (
"CMTimeRange: {{0/1 = 0.000}, {498/600 = 0.830}}",
"CMTimeRange: {{0/1 = 0.000}, {556/600 = 0.927}}"
)
2013-02-18 13:18:20.812 app[5242:907] [AVCaptureManager.m:402] tracks array: (
"<AVAssetTrack: 0x220c21a0, trackID = 1, mediaType = vide>",
"<AVAssetTrack: 0x1cdec820, trackID = 1, mediaType = vide>"
)
Your problem is caused by the scope of your AVURLAsset instances
Since you AVURLAsset *assetClip in inside the for loop, it is not valid outside of it and neither is the tracks you extracted.
If you keep your assetClip in an array that survives the scope of your for loop, it should fix your problem