该问题的解决方法是使用FFMpeg库,将.m4a文件转换为.wav格式。以下是代码示例:
import AudioKit
import AVFoundation
// M4A文件路径
let m4aFilePath = Bundle.main.path(forResource: "input", ofType: "m4a")!
let m4aURL = URL(fileURLWithPath: m4aFilePath)
// WAV文件存储路径
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let wavFilePath = "\(documentsPath)/output.wav"
let wavURL = URL(fileURLWithPath: wavFilePath)
let asset = AVURLAsset(url: m4aURL)
let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A)
exporter?.outputFileType = AVFileType.wav
exporter?.outputURL = wavURL
// 开始转换
exporter?.exportAsynchronously(completionHandler: {
guard let status = exporter?.status else {
return
}
if status == .completed {
// 转换成功
let wavFile = try! AKAudioFile(forReading: wavURL)
let player = try! AKAudioPlayer(file: wavFile)
let mixer = AKMixer(player)
AudioKit.output = mixer
try! AudioKit.start()
player.play()
} else {
// 转换失败
print("转换失败")
}
})
下一篇:AudioKitUI和格式化