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

jfalexvijay picture jfalexvijay · Dec 9, 2010 · Viewed 7.6k times · Source

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.

Answer

jfalexvijay picture jfalexvijay · Dec 15, 2010

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.