簡體   English   中英

ios - 組合/連接多個音頻文件

[英]ios - combine/concatenate multiple audio files

我有一堆音頻文件(mp3、aac 等),我想將它們連接成 1 個音頻文件。 以前有人這樣做過嗎?

我已經這樣做了。 要進行連接,您首先需要將音頻文件加載到AVAssets 中 具體來說,您需要使用名為AVURLAssets的 AVAsset 子類,它可以加載您的 URL: Loading AVAsset 然后,您可以將每個AVAsset添加到AVMutableComposition 中,該AVMutableComposition旨在包含多個AVAssets 將其加載到AVMutableComposition ,您可以使用AVAssetExportSession將組合寫入文件。

請注意, AVAssetExportSession不會讓您對文件輸出進行太多控制(它會將音頻導出到 m4a 文件中)。 如果您需要更多地控制輸出類型,則需要使用AVAssetReaderAVAssetWriter類來執行導出而不是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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM