我正在尝试合并视频,并且代码在模拟器上运行良好。我可以合并视频,但是当我在设备上运行相同的代码时,它给了我这个异常
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'* -[__NSArrayM insertObject:atIndex:]: object cannot be nil
代码在这里:
AVMutableComposition *mixComposition = [AVMutableComposition composition]; AVMutableCompositionTrack *compositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; NSError * error = nil; NSMutableArray * timeRanges = [NSMutableArray arrayWithCapacity:videoClipPaths.count]; NSMutableArray * tracks = [NSMutableArray arrayWithCapacity:videoClipPaths.count]; for (int i=0; i<[videoClipPaths count]; i++) { AVURLAsset *assetClip = [AVURLAsset URLAssetWithURL:[videoClipPaths objectAtIndex:i] options:nil]; AVAssetTrack *clipVideoTrackB = [[assetClip tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
[timeRanges addObject:[NSValue valueWithCMTimeRange:CMTimeRangeMake(kCMTimeZero, assetClip.duration)]];
[tracks addObject:clipVideoTrackB];
}
NSLog(@"HELLO: %@", timeRanges );
[compositionTrack insertTimeRanges:timeRanges ofTracks:tracks atTime:kCMTimeZero error:&error];
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
// NSParameterAssert(exporter != nil);
NSArray *t; NSString *u;
t = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
u = [t objectAtIndex:0];
NSString *finalPath = [u stringByAppendingPathComponent:@"final.mov"];
NSURL *lastURL = [NSURL fileURLWithPath:finalPath];
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.outputURL = lastURL;
[exporter exportAsynchronouslyWithCompletionHandler:^(void){
switch (exporter.status) {
case AVAssetExportSessionStatusFailed:
NSLog(@"exporting failed");
[SVProgressHUD dismiss];
break;
case AVAssetExportSessionStatusCompleted:
NSLog(@"exporting completed");
UISaveVideoAtPathToSavedPhotosAlbum(finalPath, self, nil, NULL);
[SVProgressHUD dismiss];
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"export cancelled");
break;
}
}];