Sunday, 13 July 2014

How to add speech or voice to your project?


Follow these steps:
  1. Add reference "System.Speech" from Assemblies >> Framework tab. To know how to add reference to your project click: http://gsbprogramming.blogspot.in/2014/07/how-to-add-reference-to-your-project-in.html
  2. Add namespace:    Using System.Speech.Synthesis;
  3. Create object of SpeechSynthesizer as:
SpeechSynthesizer reader=new SpeechSynthesizer( );

  1. Now use "reader" object to enable speech as:
 reader.Speak("message");

or

reader.SpeakAsync("message");

replace message with whatever you want to speak

Difference between reader.Speak( )   and reader.SpeakAsync( )  is that reader.Speak( ) will speak synchronously i.e. when speaking is finished only then next statements will execute while in reader.SpeakAsync( ) speaking is asynchronously i.e. speaking will be carried out on background and next statements can be executed simultaneously .


Note:  If  you are using reader.SpeakAsync( ) on button click then please use :
reader.Dispose( );
reader=new SpeechSynthesizer( );
and then
reader.SpeakAsync("message");

because when your click the button more than once continously speaking will be continued as many time as user has clicked the button
so to avoid this please use above .

No comments:

Post a Comment