开发者

[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array from AVURLAsset

开发者 https://www.devze.com 2023-01-30 06:17 出处:网络
I tried with following code to merge the audios. AVMutableComposition* composition = [AVMutableComposition composition];

I tried with following code to merge the audios.

AVMutableComposition* composition = [AVMutableComposition composition];
AVURLAsse开发者_高级运维t* 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];

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'

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.


I resolved this issue;

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]

Thanks.

0

精彩评论

暂无评论...
验证码 换一张
取 消