问题描述
我用下面的code试图合并音频。
I tried with following code to merge the audios.
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 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]这是给下面的错误
When I try to execute the "[[audioAsset1 tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0]" it is giving following error
*** Terminating app due to uncaught exception 'NSRangeException',
reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
但是,当我尝试打印audioAsset1控制台,它是印刷< AVURLAsset:0x198920,URL ='路径'>中。我曾与路径检查,该文件是存在的。
But when I try to print the audioAsset1 in console, it is printing "<AVURLAsset: 0x198920, URL = 'path'>". I have checked with the path, that file is there.
什么是我的错?请帮我解决这个问题。
what is my mistake? Please help me to resolve it.
推荐答案
我解决了这个问题;
我刚刚更换了以下语句结果
Just I have replaced the following statement
NSURL *audioURL = [NSURL URLWithString:path];
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:audioURL options:nil]
与下面的语句结果
with following statement
NSURL *audioURL = [NSURL fileURLWithPath:path]
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:audioURL options:nil]
感谢。
这篇关于[NSMutableArray的objectAtIndex:]:索引0超越界限为空数组从AVURLAsset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!