语音合成器“输入字符串格式不正确”

这是我的代码:

string _message = "Hello world."; SpeechSynthesizer _synth = new SpeechSynthesizer(); Prompt _prompt = new Prompt(_message); _synth.Speak(_prompt); 

我不能为我的生活弄清楚究竟是什么导致了这个错误:

“输入字符串的格式不正确。”

导致此错误的行是我调用_synth.Speak(_prompt);
编辑:我已经在我的桌面计算机上尝试了这个代码,它工作正常,所以在我的笔记本电脑上安装有问题。 我还不太确定如何解决这个问题……

编辑:

堆栈跟踪:

 System.FormatException: Input string was not in a correct format. at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffe r& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo in fo) at System.Speech.Internal.SapiAttributeParser.GetCultureInfoFromLanguageStrin g(String valueString) at System.Speech.Synthesis.VoiceInfo..ctor(VoiceObjectToken token) at System.Speech.Internal.Synthesis.VoiceSynthesis.BuildInstalledVoices(Voice Synthesis voiceSynthesizer) at System.Speech.Internal.Synthesis.VoiceSynthesis..ctor(WeakReference speech Synthesizer) at System.Speech.Synthesis.SpeechSynthesizer.get_VoiceSynthesizer() at System.Speech.Synthesis.SpeechSynthesizer.Speak(Prompt prompt) at TTSTesting.Program.Speak(String _message) in C:\Users\ctanaka\Desktop\TTST esting\TTSTesting\Program.cs:line 22 

您机器的注册表搞砸了,它包含无效的语音配置数据。 相关键是HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens 。 在下面,你会找到已安装的声音。 英语机器通常有MS-Anna,但如果您购买更多,可以有其他机器。

混乱的值是Attribute \ Language,它不是像它应该的hex数。 与“409”类似,LCID的hex值为英语。

您可以通过卸载添加的语音,删除注册表中的错误语音或修复语言值来解决此问题。 重新安装很棘手,这是Vista及更高版本Windows安装的一部分。 如果你不能修复它,你需要来自superuser.com的帮助。 或者您的安装DVD。

我决定进入声音密钥寄存器,在备份该密钥后,我逐一删除所有语音,每次尝试代码,直到错误消失。 错误发生在Loquendo语音中。 之后我尝试恢复备份(所有声音)再次寻找错误并且…操作系统回答说无法写入值,因为它正被另一个应用程序使用…这是神奇的事情:一切正常!

我有同样的问题。 Hans Passant向我们指出了这个API从注册表中解析字符串值以检测已安装的语言。 我也安装了Loquendo TTS语言包。 使用PROCMON和反复试验,我设法找出导致错误的确切注册表项(语言)。 API期望此字符串仅保留整数转换的数字字符。 尝试解析和转换此字符串(“40c”)会触发FormatException:

[HKEY_LOCAL_MACHINE \ SOFTWARE \ Wow6432Node \ Microsoft \ Speech \ Voices \ Tokens \ LQBernard \ Attributes]“Language”=“40c”

变成:

“语言”=“40”(删除尾随的“c”字符)。

我为我安装的法语Loquendo TTS语言包(Bernard和Juliette)重复了这个过程,并为我解决了这个问题。

这可能不是您的问题的根源,但它看起来像您应该能够调用的API

 new SpeechSynthesizer().Speak("Hello world."); 

并避免所有额外的代码…你试过删除期间?