gpt4 book ai didi

swift - 如何快速获取多个音频的混合并将其保存为单个音频

转载 作者:行者123 更新时间:2023-11-30 11:05:05 27 4
gpt4 key购买 nike

我有多个音频文件(超过 3 个)。通过使用 AVAudioEngineAVAudioMixerNode 我将所有音轨播放到一个轨道中。我想将混合音频保存在文档目录中。提供混合多个音频文件并保存在文档目录中的建议。

谢谢

最佳答案

试试这个:

func mergeAudioFiles(files: [URL], completion: @escaping (_ succeeded: Bool)->()) {
let composition = AVMutableComposition()
guard let compositionAudioTrack:AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid) else {
completion(false)
return
}

for fileUrl in files {
let asset = AVURLAsset(url: fileUrl, options: nil)
let assetTrack = asset.tracks(withMediaType: AVMediaType.audio)[0]
do {
try compositionAudioTrack.insertTimeRange(assetTrack.timeRange, of: assetTrack, at: compositionAudioTrack.timeRange.duration)

} catch {}
}

guard let exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A) else {
completion(false)
return
}
exporter.outputFileType = AVFileType.m4a
exporter.outputURL = URL(string: "AudioPathInDocumentsDirectory")
exporter.timeRange = compositionAudioTrack.timeRange

exporter.exportAsynchronously() {
switch exporter.status {
case .unknown,.waiting, .exporting:
print("Exported Waiting")
case .completed:
print("Exported Complete")
completion(true)
case .failed:
print("Exported Failed")
completion(false)
case .cancelled:
print("Exported Cancelled")
}
}
}

关于swift - 如何快速获取多个音频的混合并将其保存为单个音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52816376/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com