【发布时间】:2012-05-17 01:49:51
【问题描述】:
我有一堆音频文件(mp3、aac 等),我想将它们连接成一个音频文件。以前有人做过吗?
【问题讨论】:
标签: ios core-audio
我有一堆音频文件(mp3、aac 等),我想将它们连接成一个音频文件。以前有人做过吗?
【问题讨论】:
标签: ios core-audio
我已经做到了。要进行连接,您首先需要将音频文件加载到AVAssets。具体来说,您需要使用名为AVURLAssets 的 AVAsset 子类,它可以加载您的 URL:Loading AVAsset。然后,您可以将每个AVAsset 添加到一个AVMutableComposition 中,该AVMutableComposition 旨在包含多个AVAssets。将其加载到 AVMutableComposition 后,您可以使用 AVAssetExportSession 将合成写入文件。
请注意,AVAssetExportSession 无法让您对文件输出进行太多控制(它会将音频导出到 m4a 文件中)。如果您需要对输出类型进行更多控制,则需要使用AVAssetReader 和AVAssetWriter 类来执行导出,而不是使用AVAssetExportSession。这些类的使用比AVAssetExportSession 复杂得多,而且理解纯 C 是值得的。
我还要指出,没有 Apple 提供的写入 MP3 文件的选项。你可以读它们,但你不能写它们。通常最好坚持使用 m4a/aac 格式。
【讨论】:
使用 ExtAudioFile 相当简单。在伪代码中你想要做的是:
1. Open the file you'll be using for output (ExtAudioFileOpen)
2. Set the client data format you'll use for ExtAudioFileWrite
3. For each input file you want to process:
a. Open the file using ExtAudioFile
b. Set the client data format used for reading to the same client format used in the output file for writing
c. Loop through the input file using ExtAudioFileRead and save the data to the output file using ExtAudioFileWrite
d. Close the input file
4. Close the output file
【讨论】: