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

情到浓时终转凉″ 提交于 2019-11-30 23:40:31

问题


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];

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.


回答1:


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.



来源:https://stackoverflow.com/questions/4398764/nsmutablearray-objectatindex-index-0-beyond-bounds-for-empty-array-from-avur

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!