Although a bit late, butfunc saveAVSpeechUtteranceToFile(utterance: AVSpeechUtterance, fileURL: URL) throws {
let synthesizer = AVSpeechSynthesizer()
var output: AVAudioFile?
Logger.shared.debug("Saving region utterance to file: \(fileURL.absoluteString)")
try? FileManager.default.removeItem(at: fileURL)
synthesizer.write(utterance) { buffer in
guard let pcmBuffer = buffer as? AVAudioPCMBuffer else {
Logger.shared.error("unknow buffer type: \(buffer)")
return
}
if pcmBuffer.frameLength == 0 {
// no length
}else{
if output == nil {
output = try! AVAudioFile(forWriting: fileURL, settings: pcmBuffer.format.settings, commonFormat: .pcmFormatInt16, interleaved: false)
}
try! output!.write(from: pcmBuffer)
}
}
}This came from https://stackoverflow.com/questions/25965601/avspeechsynthesizer-output-as-file