[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array from AVURLAsset
我尝试使用以下代码合并音频。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | AVMutableComposition* composition = [AVMutableComposition composition]; AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:audioURL options:nil]; AVURLAsset* audioAsset1 = [[AVURLAsset alloc]initWithURL:audioURL1 options:nil]; AVURLAsset* audioAsset2 = [[AVURLAsset alloc]initWithURL:audioURL1 options:nil]; AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; NSError* error = NULL; [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,videoAsset.duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo]objectAtIndex:0] atTime:kCMTimeZero AVMutableCompositionTrack *compositionAudioTrack1 = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; [compositionAudioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero,audioAsset1.duration) ofTrack:[[audioAsset1 tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0] atTime:kCMTimeZero error:&error]; AVMutableCompositionTrack *compositionAudioTrack2 = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; [compositionAudioTrack2 insertTimeRange:CMTimeRangeMake(kCMTimeZero,audioAsset2.duration) ofTrack:[[audioAsset2 tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0] atTime:kCMTimeZero error:&error]; |
当我尝试执行 "[[audioAsset1 trackingWithMediaType:AVMediaTypeAudio]objectAtIndex:0]" 时,会出现以下错误
1 2 | *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array' |
但是当我尝试在控制台中打印 audioAsset1 时,它正在打印 "
我的错误是什么?请帮我解决它。
我解决了这个问题;
只是我已经替换了下面的语句
1 2 | NSURL *audioURL = [NSURL URLWithString:path]; AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:audioURL options:nil] |
带有以下语句
1 2 | NSURL *audioURL = [NSURL fileURLWithPath:path] AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:audioURL options:nil] |
谢谢。