I want to use google text to speech in my windows form application, it will read a label. I added System.Speech reference. How can it read a label with a button click event?
http://translate.google.com/translate_tts?q=testing+google+speech This is the google text to speech api, or how can I use microsoft’s native text to speech?
I want to use google text to speech in my windows form application, it
Share
UPDATE Google’s TTS API is no longer publically available. The notes at the bottom about Microsoft’s TTS are still relevant and provide equivalent functionality.
You can use Google’s TTS API from your WinForm application by playing the response using a variation of this question‘s answer (it took me a while but I have a real solution):
NOTE: The above code requires NAudio to work (free/open source) and
usingstatements forSystem.Web,System.Threading, andNAudio.Wave.My
Form1has 2 controls on it:label1button1with aTagoflabel1(used to bind the button to its label)The above code can be simplified slightly if a you have different events for each button/label combination using something like (untested):
There are problems with this solution though (this list is probably not complete; I’m sure comments and real world usage will find others):
stop.WaitOne(10000);in the first code snippet. The 10000 represents a maximum of 10 seconds of audio to be played so it will need to be tweaked if your label takes longer than that to read. This is necessary because the current version of NAudio (v1.5.4.0) seems to have a problem determining when the stream is done playing. It may be fixed in a later version or perhaps there is a workaround that I didn’t take the time to find. One temporary workaround is to use aParameterizedThreadStartthat would take the timeout as a parameter to the thread. This would allow variable timeouts but would not technically fix the problem.To answer the other side of your question:
The
System.Speech.Synthesis.SpeechSynthesizerclass is much easier to use and you can count on it being available reliably (where with the Google API, it could be gone tomorrow).It is really as easy as including a reference to the
System.Speechreference and:This just works.
Trying to use the Google TTS API was a fun experiment but I’d be hard pressed to suggest it for production use, and if you don’t want to pay for a commercial alternative, Microsoft’s solution is about as good as it gets.