如何在 swift 中将 .m4a 音频文件转换为 .caf 文件

问题描述 投票:0回答:0

我必须将扩展名为 .m4a 的音频文件转换为 .caf。我已经准备好并可以使用代码,但是这样做时我只得到文件的一部分(不完整的 .caf 文件)。

以下函数用于将文件从.m4a 转换为.caf。 这里,inputURL 是.m4a 文件,outputURL 是.caf 扩展名:

 func convertAudioFileToCaf(inputURL: URL, outputURL: URL, completion: @escaping (Bool) -> Void) {
            let asset = AVURLAsset(url: inputURL, options: nil)
            let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetPassthrough)
            exportSession?.outputURL = outputURL
        exportSession?.outputFileType = AVFileType.caf
            exportSession?.exportAsynchronously(completionHandler: {
                switch exportSession!.status {
                case .completed:
                    completion(true)
                case .failed:
                    print("failed \(String(describing: exportSession?.error))")
                    completion(false)
                case .cancelled:
                    print("cancelled \(String(describing: exportSession?.error))")
                    completion(false)
                default:
                    break
                }
            })
    }
swift core-audio avaudioengine audio-converter avaudiofile
© www.soinside.com 2019 - 2024. All rights reserved.