如何使用AVAssetExportSession将wav文件导出到22kHz的m4a?

时间:2011-09-06 10:52:17

标签: ios audio wav m4a avassetexportsession

我有一个22kHz的波形文件,想要一个22kHz的m4a文件。具有预设AVAssetExportPresetAppleM4A的AVAssetExportSession会自动将我的wav转换为44kHZ。我尝试了不同的预设和零来创建ExportSession,但没有成功。

有没有办法设置AVAssetExportSession的自定义导出属性,还是需要一种完全不同的方法,如How to convert WAV file to M4A?中所述的方法?

这是我到目前为止的代码,如果你想要一个44kHz的文件,这个代码很有用:

AVURLAsset *wavAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:wavPath] options:optionsDict];

AVMutableComposition *mutableComposition = [AVMutableComposition composition];
[mutableComposition insertTimeRange:CMTimeRangeMake(kCMTimeZero, wavAsset.duration)
  ofAsset:wavAsset atTime:kCMTimeZero error:&error];

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
  initWithAsset:[mutableComposition copy] presetName:AVAssetExportPresetAppleM4A];
exportSession.outputURL = [NSURL fileURLWithPath:m4aPath];
exportSession.outputFileType = AVFileTypeAppleM4A;

[exportSession exportAsynchronouslyWithCompletionHandler:^{
  switch (exportSession.status) {
    case AVAssetExportSessionStatusCompleted: {
      [exportSession release];
      completionHandler(nil);
      break;
    }
    case AVAssetExportSessionStatusFailed: {
      NSLog(@"There was an error while exporting the file: %@", exportSession.error);
      completionHandler(exportSession.error);
      break;
    }
    // ... handle some other cases...
    default: {
      break;
    }
  }
}];

如果我错过了一些东西会很棒。

提前致谢, DOM

1 个答案:

答案 0 :(得分:0)

我对TPAACAudioConverter提供了一些运气here。它使用AudioToolbox Framework中的ExtendedAudioFile API。