Coding for the Speak Button using the SpeechSynthesizer Class

Text to Speech: For C# and VB NET Students

 

We coded for the Form Load event, in the previous lesson. Out Combo Box is nicely populated with Voices installed on the system. We can now write code for the Speak. Button. When this button clicked, whatever is in the text box will get read out.

Actually, there's not much to the speak code. It's fairly easy!

Double-click your BtnSpeech to create a code stub. Set up two string variables:

C#

string voice = cmbVoice.Text;
string theText = txtSpeechText.Text;

VB Net

Dim voice As String = cmbVoice.Text
Dim theText As String = txtSpeechText.Text

The first string variable has the name voice. We're storing whatever is the Text property of the combo box into the voice string. The combo box contains all the available voices on the computer, remember. By using the Text property, we can get the chosen voice.

The next string variable gets the contents of the text box and stores it into a variable called theText.

Next, we can do some error checking.

The default text for the combo box is "Select Voice". If it's still that when the Speak button is clicked, then it means no voice was chosen. In which case, we can display a message box and exit. Here's the code for C#:

if (voice == "Select Voice")
{

MessageBox.Show("Select a Voice");
return;

}

And here it is for VB Net:

If voice = "Select Voice" Then

MessageBox.Show("Select a Voice")
Exit Sub

End If

We can check for a blank text box, as well. Add this If statement in C#:

if (theText == "")
{

MessageBox.Show("Type some text");
return;

}

And this in VB:

If theText = "" Then

MessageBox.Show("Type some text")
Exit Sub

End If

Now create a new SpeechSynthesizer object:

C#

synthVoice = new SpeechSynthesizer();

VB

synthVoice = New SpeechSynthesizer()

To make sure that you can hear something, you set the speech output to the default audio device. Add this line (delete the semicolon in VB Net):

synthVoice.SetOutputToDefaultAudioDevice();

So you just call the SetOutputToDefaultAudioDevice Sub/method of the synthVoice object.

Next, we can use a Sub/method called SelectVoice. Add this line (again, without the semicolon in VB Net):

synthVoice.SelectVoice(voice);

In between the round brackets of SelectVoice, you need one of those voice names from the combo box. Ours is stored in the voice variable. You can, if you want, hard code a voice. You'd do it like this:

synthVoice.SelectVoice("Microsoft Zira Desktop");

Just replace Zira with a voice name you have on your computer.

Next, we can set a speed rate for the voice. This, if you remember, goes from -10 to 10. We used a Track Bar control to set the rate. Add this line in C#:

synthVoice.Rate = trackBar1.Value;

And this in VB Net:

synthVoice.Rate = TrackBar1.Value

We use the Rate property of synthVoice, which takes an Integer value. The track bar has a Value property that is also an Integer, so you can just assign it to the Rate of synthVoice.

We can do the same with the volume. Add this in C#:

synthVoice.Volume = trackBar2.Value;

And this in VB Net:

synthVoice.Volume = TrackBar2.Value

Again, we're getting the Value from the TrackBar control. This can go straight into the Volume property of synthVoice as they are both Integers.

Finally, we can issue the Speak command. There are two Speak commands to choose from. One is just Speak, and the other is SpeakAsync. The Speak one is useful for short phrases. You can't stop it, or do anything else, until it's finished speaking. The SpeakAsync one is the one we'll use for our Speak button. It means you can do other things while the text is being spoken. We'll want the opportunity to pause, resume, and stop the voice. You won't be able to click these buttons if you just use Speak.

Add this line in to your code (without the semicolon in VB Net):

synthVoice.SpeakAsync(theText);

In between the round brackets of SpeakAsync, you type the text you want the voice to speak. Our text is in the variable theText, which we got from the text box. If you wanted to hard code a line, you'd do it like this:

synthVoice.SpeakAsync("Say something, please!");

Before trying it out, create a FormClosing event, just like you did for the Form Load event. Now dispose of the synthVoice object (no semicolon in VB Net):

synthVoice.Dispose();

Your code for BtnSpeech should look like this in C#:

C# code for text to speech

And this in VB Net:

VB Net code for text to speech on a Windows form

Try it out. Select a voice from your dropdown list. Type some text into your text box. Now click your Speak button. You should hear your chosen voice speak whatever you entered into text box text, assuming you have a speaker attached to your computer.

Try out your two sliders. Adjust the volume and the voice speed. They won't change while the text is being spoken, so you'll have to wait until it's finished.

OK, now we can code for the Pause, Resume, and Stop buttons. We'll do that in the next lesson.

Pause, Resume, Stop Buttons >>

Back to the C# NET Contents Page

Back to the VB NET Contents Page