关于ios:ios-合并/连接多个音频文件

ios - combine/concatenate multiple audio files

我有一堆音频文件(mp3,aac等),我想将它们连接成1个音频文件。 有人做过吗?


我已经做到了。要进行串联,首先需要将音频文件加载到AVAsets中。具体来说,您将要使用称为AVURLAssets的AVAsset的子类,该子类可以加载您的URL:加载AVAsset。然后,您可以将每个AVAsset添加到AVMutableComposition中,该AVMutableComposition设计为包含多个AVAssets。将其加载到AVMutableComposition中后,可以使用AVAssetExportSession将组合写入文件中。

请注意,AVAssetExportSession并不能使您对文件输出进行太多控制(它将音频输出到m4a文件中)。如果需要对输出类型进行更多控制,则需要使用AVAssetReader和AVAssetWriter类执行导出,而不是使用AVAssetExportSession。这些类比AVAssetExportSession使用起来要复杂得多,在这里需要了解直接的C语言。

我还将指出,Apple没有提供写出MP3文件的选项。您可以阅读它们,但不能编写它们。通常最好坚持使用m4a / aac格式。


使用ExtAudioFile相当简单。使用伪代码,您想要做的是:

1
2
3
4
5
6
7
8
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