AVAssetExportSession - AVMutableCompositionTrack - 导出的输出文件为空

时间:2010-10-23 16:14:30

标签: iphone audio avfoundation

我尝试将.mp3文件添加到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:^{

 }];

提前谢谢!

1 个答案:

答案 0 :(得分:2)

就像我在评论中写的那样,它与不同的文件格式有关。我将我的文件更改为.m4a以及代码 - 因此所有内容(此操作的来源和目标)都与.m4a相关并且有效。

顺便说一句:我还尝试使用.wav文件,但是在使用wav进行操作时会发生奇怪的事情。我不推荐它。