我可以在iOS 6中成功地将2个视频合并为1个。但是我不知道iOS 7中发生了什么。我得到了这样的数组错误。
由于未捕获的异常'NSRangeException'而终止应用程序,原因:' * - [__ NSArrayM objectAtIndex:]:索引0超出空数组的边界
这是我编写的代码的一部分。我已经在代码中显示了错误。我该怎么办?
NSMutableArray *array = [[NSMutableArray alloc]init ];
for(int kk=0;kk < trackRecordingVideoName+1; kk++)
{
[array addObject:[[self userPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%d.mp4",recordingVideoName,kk]]];
NSLog(@"%d is added",kk);
}
videoPathArray= [[NSArray alloc]initWithArray:array];
[videoPathArray retain];
AVMutableComposition *composition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
CMTime startTime = kCMTimeZero;
NSLog(@"videoPathArray.count is %d",videoPathArray.count);
for (NSInteger i=0; i < videoPathArray.count; i++) {
NSLog(@"For loop now is %d and name is %@",i,[videoPathArray objectAtIndex:i]);
NSString *path = (NSString*)[videoPathArray objectAtIndex:i];
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
[url release];
//*************************************
AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];//This is the error
//*************************************
if(i == 0)
{
[compositionVideoTrack setPreferredTransform:videoTrack.preferredTransform];
}
Boolean ok = [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset duration]) ofTrack:videoTrack atTime:startTime error:nil];
if(ok)
{NSLog(@"can combine in for loop");}
else{NSLog(@"cannot combine in for loop");}
startTime = CMTimeAdd(startTime, [asset duration]);
}
答案 0 :(得分:0)
检查url
是否为正确,它应指向真实的视频文件,AVURLAsset
不包含任何视频媒体类型。