我嘗試將.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:^{
}];
預先感謝您!
啊哈,它與使用的文件格式有關。例如,如果我在(例如)文件中創建了所有的東西(源文件,目標文件, WAV格式,那麼它的作品!必須嘗試更多,並將結果發佈... – Micko 2010-10-23 16:59:56