gpt4 book ai didi

ios - AVSpeechSynthesizer 音量太低

转载 作者:行者123 更新时间:2023-11-30 10:59:02 24 4
gpt4 key购买 nike

我正在尝试使用 Swift 创建一个应用程序。

我正确集成了语音转文本和文本转语音:我的应用程序运行完美。你可以找到我的项目here .

语音转文本后,应用程序向服务器发出 http 请求(发送识别的文本),并以声音方式再现响应(它是一个字符串,即:“好的,我将向您展示一些内容”)从文本到语音。但是,有一个大问题,我无法解决。

当应用程序以声音方式再现文本时,声音太慢,就好像它在后台一样,好像有什么比声音更重要的东西要再现(实际上什么也没有)。

调试后,我发现问题是在函数 recordAndRecognizeSpeech() 中使用audioEngine (AVAudioEngine) 开始的。在不使用此功能的情况下运行应用程序并播放随机文本,它就像一个魅力。

所以,在我看来,当应用程序以声音方式再现文本时,它认为仍然存在事件的音频引擎,因此音量非常慢。

但是,在复制文本之前,我调用了这些函数(查看 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: 必需条件为 false: [AVAudioIONodeImpl.mm:910:SetOutputFormat: (IsFormatSampleRateAndChannelCountValid(hwFormat))]这个异常是由.setCategory(AVAudioSession.Category.ambient)引起的,它应该是.playAndRecord,但是使用这个值音量又变低了。

最佳答案

试试这个。

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)
}
}
}

关于ios - AVSpeechSynthesizer 音量太低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53619027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com