问题描述
我在AVFoundation中使用语音合成器,创建了这样的声音实例:
I'm using the speech synth in AVFoundation, creating an instance of a voice like this:
import AVFoundation
class CanSpeak {
let voices = AVSpeechSynthesisVoice.speechVoices()
let voiceSynth = AVSpeechSynthesizer()
var voiceToUse: AVSpeechSynthesisVoice?
init(){
for voice in voices {
if voice.name == "Arthur"
{
voiceToUse = voice
}
}
}
func sayThis(_ phrase: String){
let utterance = AVSpeechUtterance(string: phrase)
utterance.voice = voiceToUse
utterance.rate = 0.5
voiceSynth.speak(utterance)
}
}
我有两个问题.
- 没有并发.多次调用此函数会导致要排队的字符串排队.我不要如果对此功能有多个调用,请将它们关闭,我希望声音立即开始说话,即使那意味着要自己说话.我如何做到这一点?
2-加载时我不理解的一些奇怪错误:
2 - some odd errors that I don't understand on loading:
推荐答案
至于#1,它可能不会发生.语音合成器是系统共享的资源,因此,作为API的客户端,系统如何处理调度多个请求是我们无法控制的. (请注意,如果您重复使用同一台合成器,则会将额外的语音排队,但是如果您创建多个合成器,则它无法说出另一台合成器正在讲话时所要求的语音.)
As for #1, it's probably not gonna happen. The speech synthesizer is a system shared resource, so how the system handles scheduling multiple requests is out of our control as clients of the API. (Note that if you reuse the same synthesizer, it queues up extra utterances, but if you create multiple synthesizers, it fails to speak utterances that are requested while another synthesizer is speaking.)
Dunno关于#2,对不起.看起来像诊断文本,不一定是错误.大概值得提交错误,因为他们可能不想在没有实际问题时记录诊断信息.
Dunno about #2, sorry. Looks like diagnostic text, not necessarily an error. Probably worth filing a bug about, since they probably don't want to be logging diagnostics when there's no actual problem.
奖励答案:您可以使用函数式编程来简化语音选择:
Bonus answer: You can use functional programming to make the selection of voice a bit shorter:
let voice = AVSpeechSynthesisVoice.speechVoices().first(where: { $0.name == "Arthur" })
这篇关于iOS上的语音合成在加载时出现奇怪的错误,并且没有并发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!