我正试图用Swift创建一个应用程序。
我正确地集成了语音到文本和文本到语音:我的应用程序工作得很完美。您可以找到我的项目这里。
在语音到文本之后,应用程序向服务器发出http请求(发送识别的文本),响应(它是一个字符串,即:“好的,我会给你看一些东西”)从文本复制到语音。但是,有一个大问题,我解决不了。
当应用程序用声音复制文本时,声音太慢了,就像在背景中一样,好像有什么东西比声音更重要(实际上什么都没有)。
调试时,我发现这个问题开始在函数audioEngine (AVAudioEngine)中使用recordAndRecognizeSpeech()。运行该应用程序时不使用此功能并随机播放文本,它的工作原理就像一种魅力。
所以,在我看来,当应用程序在声音上复制文本时,它认为仍然有活动的audioengine,所以音量非常慢。
但是,在复制文本之前,我调用了这些函数(查看ac函数,第96行):
audioEngine.stop()
audioEngine.reset()我怎样才能解决这个问题?
编辑:
我找到了一个部分的解决方案。现在,在应用程序语音播放文本之前,我的代码是:
audioEngine.inputNode.removeTap(onBus: 0)
audioEngine.stop()
audioEngine.reset()
recognitionTask?.cancel()
isRecording = false
microphoneButton.setTitle("Avvia..", for: UIControl.State.normal);
do {
let audioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(AVAudioSession.Category.ambient)
try audioSession.setActive(false, options: .notifyOthersOnDeactivation)
} catch {
print(error)
}
make_request(msg: self.speech_result.text!)函数.setCategory工作,卷类似于默认的卷。当我尝试回忆recordAndRecognizeSpeech()函数时,应用程序给了我这个例外:
VAEInternal.h:70:_AVAE_Check: required condition is false: [AVAudioIONodeImpl.mm:910:SetOutputFormat: (IsFormatSampleRateAndChannelCountValid(hwFormat))]这个异常是由.setCategory(AVAudioSession.Category.ambient)引起的,它应该是.playAndRecord,但是有了这个值,卷返回值就会很低。
发布于 2020-07-11 13:34:22
试试这个。
let speaker = AVSpeechSynthesizer()
func say(text: String, language: String) {
// Start audio session
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSession.Category.playAndRecord)
try audioSession.setMode(AVAudioSession.Mode.default)
try audioSession.setActive(true)
try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSession.PortOverride.speaker)
} catch {
return
}
if speaker.isSpeaking {
speaker.stopSpeaking(at: .immediate)
} else {
myUtterance = AVSpeechUtterance(string: text)
myUtterance.rate = AVSpeechUtteranceDefaultSpeechRate
myUtterance.voice = AVSpeechSynthesisVoice(language: language)
myUtterance.pitchMultiplier = 1
myUtterance.volume = 2
DispatchQueue.main.async {
self.speaker.speak(myUtterance)
}
}
}发布于 2018-12-05 05:41:51
试试这个。快速设定播放率
var speedd = AVSpeechSynthesizer()
var voicert = AVSpeechUtterance()
voicert = AVSpeechUtterance(string: "Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon")
voicert.voice = AVSpeechSynthesisVoice(language: "en-US")
voicert.rate = 0.5
speedd.speak(voicert)https://stackoverflow.com/questions/53619027
复制相似问题