目标:对话流程语音Bot Api
我需要将wav文件发送到Dialog Flow Api,并且格式和设置是预先定义的。
AVAudioRecorder
格式使用.wav
录制了音频,使用以下设置audioFilename = getDocumentsDirectory().appendingPathComponent("input.wav")
let settings: [String: Any] = [
AVFormatIDKey: Int(kAudioFormatLinearPCM),
AVSampleRateKey: 16000,
AVNumberOfChannelsKey: 2,
AVLinearPCMBitDepthKey: 16,
AVLinearPCMIsBigEndianKey: false,
AVEncoderAudioQualityKey: AVAudioQuality.max.rawValue
]
do {
audioRecorder = try AVAudioRecorder(url: audioFilename!, settings: settings)
audioRecorder.isMeteringEnabled = true
audioRecorder.prepareToRecord()
audioRecorder.delegate = self
audioRecorder.record()
recordButton.setTitle("Tap to Stop", for: .normal)
} catch {
print(error.localizedDescription)
finishRecording(success: false)
}
}
Base64
音频格式let outputFile = try Data.init(contentsOf: fileUrl)
let base64String = outputFile.base64EncodedString(options: NSData.Base64EncodingOptions.init(rawValue: 0))
print(base64String)
因此,每当我尝试使用online converter解码该编码的字符串时,它都会显示一些损坏的字节
想法?
因此,我找到了问题的答案。我的字节数组无法维护正确的标头的原因是因为我在settings
变量中省略了一个键
AVAudioFileTypeKey: kAudioFileWAVEType
let settings: [String: Any] = [
AVSampleRateKey: 16000,
AVNumberOfChannelsKey: 1,
AVAudioFileTypeKey: kAudioFileWAVEType, //MANDATORY
AVFormatIDKey: kAudioFormatLinearPCM,
AVLinearPCMIsBigEndianKey: false,
AVLinearPCMIsNonInterleaved: true,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
[
audioRecorder = try AVAudioRecorder(url: audioFilename!, settings: [:] /*empty settings*/)
然后
❌AVAudio刻录机将根据文件中定义的自动准备文件。 ❌但是事实证明,格式
也没有帮助😫
因此,在我使用设置的同时,我发现了这个[[非常重要键AVAudioFileTypeKey
,它有助于维护正确的标题并因此保持有效的.wav
文件😎