2010-12-09 56 views
9

我試着用下面的代碼合併音頻。[NSMutableArray objectAtIndex:]:索引0超出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]; 

當我嘗試執行 「[[audioAsset1 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]」 這是給下面的錯誤

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

但是,當我嘗試打印audioAsset1控制檯,它正在打印「< AVURLAsset:0x198920,URL ='路徑'>」。我檢查了路徑,該文件在那裏。

我的錯誤是什麼?請幫我解決它。

+0

請問我們能否看到`audioURL1`的值? – 2010-12-09 14:17:12

+0

我檢查了調試模式。我從audioURL獲取它。不在audioURL1中。 – jfalexvijay 2010-12-09 14:34:09

回答

33

我解決了這個問題;

只是我已經更換了如下聲明

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

與下面的語句

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

感謝。

相關問題