2010-10-23 45 views
1

我嘗試將.mp3文件添加到AVMutableCompositionTrack,之後我想導出新文件。問題是:生成的文件在導出後存在,但它是空的,無法播放。有人在我的代碼中看到錯誤嗎?AVAssetExportSession - AVMutableCompositionTrack - 導出的輸出文件爲空

AVMutableComposition *saveComposition = [AVMutableComposition composition]; 
NSArray *docpaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *tempPath = [docpaths objectAtIndex:0]; 

NSLog(@"Temporary Path: %@", tempPath); 

NSString *audioPath = [[NSBundle mainBundle] pathForResource: @"1" ofType: @"mp3"]; 
NSURL *audioUrl = [[NSURL alloc] initFileURLWithPath:audioPath]; 
AVURLAsset *audio = [AVURLAsset URLAssetWithURL:audioUrl options:nil]; 
NSLog(@"%@", audio); 
[audioUrl release]; 
AVMutableCompositionTrack *compositionAudioTrack = [saveComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
AVAssetTrack *clipAudioTrack = [[audio tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 

[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [audio duration]) ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil]; 

NSString *path = [tempPath stringByAppendingPathComponent:@"mergedaudio.m4a"]; 
if([[NSFileManager defaultManager] fileExistsAtPath:path]) 
{ 
    [[NSFileManager defaultManager] removeItemAtPath:path error:nil]; 
} 
NSURL *url = [[NSURL alloc] initFileURLWithPath: path]; 

AVAssetExportSession *exporter = [[[AVAssetExportSession alloc] initWithAsset:saveComposition presetName:AVAssetExportPresetAppleM4A] autorelease]; 
exporter.outputURL=url; 
[exporter setOutputFileType:@"com.apple.m4a-audio"]; 

NSLog(@"%@", [exporter supportedFileTypes]); 
exporter.outputFileType=[[exporter supportedFileTypes] objectAtIndex:0]; 

[exporter exportAsynchronouslyWithCompletionHandler:^{ 

}]; 

預先感謝您!

+0

啊哈,它與使用的文件格式有關。例如,如果我在(例如)文件中創建了所有的東西(源文件,目標文件, WAV格式,那麼它的作品!必須嘗試更多,並將結果發佈... – Micko 2010-10-23 16:59:56

回答

2

就像我在評論中寫到的那樣,它與不同的文件格式有關。我將文件更改爲.m4a以及代碼 - 因此,所有內容(此操作的來源和目標)都與.m4a相關並且可以工作。順便說一句:我也嘗試使用.wav文件,但在使用wav進行操作時會發生奇怪的事情。我不推薦它。

+0

我想在Nsdata中保存歌曲數據以便上傳到服務器,您可以幫助嗎? – GhostRider 2010-10-27 09:17:00

+1

對不起,我從來沒有使用NSData或服務器連接... – Micko 2010-10-31 20:31:23