2010-12-09 15 views
9

Ich versuchte mit folgendem Code, um die Audios zusammenzuführen.[NSMutableArray objectAtIndex:]: Index 0 über Grenzen für leere Array von AVURLAsset

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

Wenn ich versuche, die auszuführen "[[audioAsset1 tracksWithMediaType: AVMediaTypeAudio] objectAtIndex: 0]" wird geben folgende Fehler

*** Terminating app due to uncaught exception 'NSRangeException', 
reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'

Aber wenn ich versuche, die audioAsset1 in der Konsole zu drucken, druckt "< AVURLAsset: 0x198920, URL = 'Pfad' >". Ich habe mit dem Pfad überprüft, dass die Datei da ist.

Was ist mein Fehler? Bitte helfen Sie mir, es zu lösen.

+0

können wir bitte den Wert von 'audioURL1' sehen? –

+0

Ich habe im Debug-Modus überprüft. Ich bekomme es von audioURL. nicht in audioURL1. – jfalexvijay

+0

Wenn ich versuche, "[audioAsset1 tracksWithMediaType: AVMediaTypeAudio]" zu drucken, wird folgende Anweisung gedruckt; "asset: ( )" - es bedeutet, Asset ist leer. – jfalexvijay

Antwort

33

Ich löste dieses Problem;

Gerade habe ich die folgende Anweisung
ersetzt

NSURL *audioURL = [NSURL URLWithString:path]; 
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:audioURL options:nil]

mit folgenden Anweisung

NSURL *audioURL = [NSURL fileURLWithPath:path] 
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:audioURL options:nil]

Dank.

+0

Sie sind ein Lebensretter! – IanStallings

+2

Ich habe es anders gemacht und es hat mein Problem gelöst. Ich weiß nicht wie. :) –

+0

Bravo! Eine Stunde gespeichert. – AVEbrahimi