AVAssetTrack合并视频不可以在iOS 7中



我可以在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]);
}

检查url是否正确,它应该指向一个真正的视频文件,您的AVURLAsset不包含任何视频媒体类型

最新更新