In this application it is discussed how you can use Windows Narrator to create talking application.
It is really easy and fun to create applications that speaks out for
you. With the introduction with System.Speech, it seems to look like
even better. It is to be noted, that Windows Environment comes with a
unique feature that converts your voice commands to text equivalent
long before the introduction to this in .NET. I will discuss now how
you can easily use this in your application and get the work done
Windows Speech Recognization Engine and Narrator utility comes with
Windows XP service pack 2 or Office applications. Windows Narrator uses
various windows items and speaks instantly. If you have never tried
with it, it is time to try it first.
Open Start - > Programs -> Accessories - >Accessibility -> Narrator
After opening you will hear that a person is speaking out some weird
terms. Actually this is nothing but your narrator speaking to help you.
Actually the checkboxes determines various options like, reading
events, Read typed characters etc. You can even change the narrator
just by clicking on the Voice Button.
Here you can see Microsoft Sam is listed. This is the default .NET
narrator. You can adjust speed, volume, pitch etc of the listed
Narrators. You can also add some external Narrators for you if you
wish. Now I am going to show you how you can invoke Narrator to be used
in your own application.
If you are not using .NET Framework 3.0 or above, this must be your
only way to do this. First in this case you need to install Speech SDK
5.1. The speech SDK gives you the interface to work with SPVoice
commands. The Speech SDK provides you two interfaces. One is for Text
to Speech Conversion, while the other is for Speech Recognisation. In
this article I am going to discuss only Text to Speech Conversion.
To do this, first create the Interop to the COM dll that comes with
SAPI named speechlib.dll. You need to right click on your project, add
Reference and add "Microsoft Speech Object Library"
from COM tab.
After you add Interop.Speechlib.dll in your application you need to use
some simple lines of code as below to run your application :
SpVoice speech = new SpVoice();
Thus if you run the above application, you will find that Microsoft Sam will say you "Hello World".
With the Introduction of .NET Framework 3.0, Microsoft released Managed
API to handle these through your program and it doesn't need anything to
be installed. Let us look how we can invoke using the new way.
First Add Reference to System.Speech.dll. You will find that in GAC if you have installed .NET 3.5 to your system.
As shown in the figure, after you add
the dll you need to use some simple methods to invoke Speech To Text
for your application.
To invoke this, you need to know
about a class called SpeechSynthesizer. This is the main class to
handle speech requests from the application and invokes the Narrator
utility itself with configured narrator.
can configure voice, speech, pitch of the narrator, and change the
Narrator voice itself using the object of SpeechSynthesizer class.
Let us see how the code look like :
SpeechSynthesizer spsynthesizer = new SpeechSynthesizer();
List lstVoice = this.LoadSelectVoiceMenu();
private List LoadSelectVoiceMenu()
List lst = new List();
foreach (InstalledVoice voice in spsynthesizer.GetInstalledVoices())
Now when you want to invoke the Speaking utility just call :
spsynthesizer.SelectVoice(lstVoice.Name); //Invoke the one which you want
spsynthesizer.Volume = 50; // Can be 1 - 100
spsynthesizer.Rate = 3; // can be 1 - 10
The SelectVoice method is used to choose the appropriate Narrator. If
you have more than one narrator, you can use this method to
SelectVoice. I have used lstVoice which should point to Microsoft
Sam. The volume determines the volume of the voice, and Rate determines
how fast the narrator speaks.
To Speak out the text, you have two method. I am using SpeakAsync, to
call it asynchronously. You can also use Speak itself, which will wait
until the text is spoken. While the text is been spoken using
SpeakAsync, you can all SpeakAsyncCancel or SpeakAsyncCancelAll anytime
to end the narrator instantly.
spsynthesizer.SpeakAsyncCancel(new Prompt("Lets End"));
if invoked will end the SpeakAsync and prompt Lets End.
Thus invoking a Text to Speech for your application become easiest like never before. I will discuss more about it in my next article in this topic.
Thank you for reading.
If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.