我尝试使用以下代码合并音频。
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];
当我尝试执行“[[[audioAsset1tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]”时,出现以下错误
*** Terminating app due to uncaught exception 'NSRangeException',
reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
但是,当我尝试在控制台中打印audioAsset1时,它正在打印“”。我已经检查了路径,该文件在那里。
我的错是什么?请帮我解决。
最佳答案
我解决了这个问题;
我只是替换了以下语句
NSURL *audioURL = [NSURL URLWithString:path];
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:audioURL options:nil]
有以下陈述
NSURL *audioURL = [NSURL fileURLWithPath:path]
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:audioURL options:nil]
谢谢。
关于iphone - [NSMutableArray objectAtIndex :]: index 0 beyond bounds for empty array from AVURLAsset,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4398764/