最近在做一个文字转语音的小工具,目前已经上架有音册,抽空做了记录,有需要的小伙伴自取哈~
一、在iOS13之前
需要借助苹果的语音播报AVSpeechUtterance,外加一个录音AVAudioRecorder;说白了就是录制手机的播报音;
播报相关代码:
let speaker = AVSpeechSynthesizer() speaker.delegate = self // 播放文字 let utterance = AVSpeechUtterance(string: txt) utterance.rate = rate utterance.pitchMultiplier = pitchMultiplier // 设置音量 utterance.volume = volume // 播报前停顿 utterance.preUtteranceDelay = 0.1 // 播报后停顿 utterance.postUtteranceDelay = 0.1 guard let voice = AVSpeechSynthesisVoice(language: language) else { return } utterance.voice = voice // 播报 speaker.speak(utterance)
录音就没啥好说的,常规操作,注意采样率
let root = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! let path = root + "/" + recordFile let url = URL(fileURLWithPath: path) let sets: [String: Any] = [ AVFormatIDKey: NSNumber(value: kAudioFormatMPEG4AAC), AVSampleRateKey: NSNumber(value: 44100), AVNumberOfChannelsKey: NSNumber(value: 1), AVLinearPCMBitDepthKey: NSNumber(value: 16), AVEncoderAudioQualityKey: NSNumber(value: AVAudioQuality.high.rawValue) ] let recorder = try! AVAudioRecorder(url: url, settings: sets) recorder.isMeteringEnabled = true recorder.delegate = self // 开始录音 recordAudio()
实现AVAudioRecorder代理即可
录音的方式难免会把环境中的声音录进去,因此可能需要二次处理。
二、在iOS13之后
AVSpeechUtterance提供了writeUtterance,一切就边的很舒服了
let root = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! let path = root + "/" + recordFile speaker.write(utterance) { [self] buffer in guard let pcmBuffer = buffer as? AVAudioPCMBuffer else { fatalError("unknown buffer type: \(buffer)") } if pcmBuffer.frameLength == 0 { // done fixed() } else { // append buffer to file if self.output == nil { let url = URL(fileURLWithPath: path) self.output = try? AVAudioFile( forWriting: url, settings: pcmBuffer.format.settings, commonFormat: .pcmFormatInt16, interleaved: false) } try? self.output?.write(from: pcmBuffer) } }
writeUtterance能够读取文本转换后的pcm数据,我们要做的就是把数据写进AVAudioFile即可
另外注意一点,录音文件格式最好是.caf,别的似乎都不太理想。
最后,祝你生活愉快 ;)