Pergunta

Here is my code:

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

I can not for the life of me figure out what exactly is causing this error:

"Input string was not in a correct format."

The line that causes this error is when I call _synth.Speak(_prompt);
EDIT: I have tried this code on my desktop computer and it works fine so something is wrong with my install on my laptop. Still I'm not too sure how to fix this...

EDIT:

The stacktrace:

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
Foi útil?

Solução

The registry of your machine is messed up, it contains invalid voice configuration data. The relevant key is HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens. Underneath, you'll find the installed voices. An English machine typically has MS-Anna there but there can be others if you purchased more.

The messed up value is Attribute\Language, it isn't an hexadecimal number like it should be. Like "409", the hex value of the LCID for English.

You might fix it by uninstalling voices you added, deleting bad voices in the registry or fixing the Language value. Reinstalling is tricky, this is part of the Windows setup on Vista and up. You'll need help from superuser.com if you can't get it fixed. Or your setup DVD.

Outras dicas

I resolved going into the voices key register and, after a backup of that key, I removed one after one all voices trying the code each time untill the error disappeared. The error was in a Loquendo voice. After that I tried to restore the backup (all voices) looking for the error again and... OS answered that a value cannot be written because it was in use by another application... That's the magic thing: all worked fine!

I had the same problem. Hans Passant pointed us to the fact that this API parses string values from the registry to detect installed languages. I too had a Loquendo TTS language packs installed. Using PROCMON and trial and error, I managed to pinpoint the exact registry key that caused the error (Language). The API expects this string to hold only numeric characters for an integer conversion. Trying to parse and convert this string ("40c") triggers a FormatException:

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

changed to:

"Language"="40" (removed the trailing 'c' character).

I repeated this process for both my installed French Loquendo TTS language packs (Bernard and Juliette) and that solved it for me.

This may not be the source of your problem, but it looks like in the API that you should just be able to call

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

and avoid all the extra code... have you tried removing the period?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top