Home and Learn: Intermediate Programming
Text to Speech: For C# and VB NET Students
We have the Text to Speech part of our program working OK, from the last lesson. You can enter something in a text box and have that text read out for you. In this lesson, you'll learn how to pause the text to speech, resume it and stop it altogether.
Double-click your Pause button to create a code stub. It's quite easy to pause a voice. Add this if statement in C#:
if (synthVoice.State == SynthesizerState.Speaking)
{
synthVoice.Pause();
}
And this one in VB Net:
If synthVoice.State = SynthesizerState.Speaking Then
synthVoice.Pause()
End If
We're checking the State property of synthVoice. After the equal sign, there is an enumeration called SynthesizerState. There is a Speaking value you can use. Under the hood, Speaking has a value of 1. So you could do this, if you wanted:
If synthVoice.State = 1 Then
synthVoice.Pause()
End If
But it's a lot easier to read with the enumeration in place.
And that's it for the Pause button.
The resume button is just as easy. Double-click your Resume button and add this in C#
if (synthVoice.State == SynthesizerState.Paused)
{
synthVoice.Resume();
}
And this in VB Net:
If synthVoice.State = SynthesizerState.Paused Then
synthVoice. Resume ()
End If
Now we're just checking for the Paused enumeration value. (it's 2, under the hood.) If it is paused then we can Resume the synthVoice.
For the stop button, we only need to dispose of the synthVoice object.
Double-click your Stop button. Add this code in C#:
if (synthVoice != null)
{
synthVoice.Dispose();
}
And this in VB Net:
If synthVoice IsNot Nothing Then
synthVoice.Dispose()
End If
We're checking if the synthVoice isn't null (or Nothing in VB). If it's not, then we can dispose of the object.
There is a problem with the code, however. If you were to run it now and hit Speak, then the Stop button, everything would work fine. But click the Pause or Resume buttons after stopping the speech and your program would crash. If would give you an error message about Cannot access a disposed object.
One way to solve this (not the way we'll do it) is by switching off buttons after the Stop button is clicked. For example:
if (synthVoice != null)
{
synthVoice.Dispose();
txtPause.Enabled = false;
txtResume.Enabled = false;
}
You'd then have to remember to set the Enabled property back to true when the Speak button is clicked.
What we'll do, though, is to set a Boolean flag. Add this to the top of your code in C#, just under the line that sets up the SpeechSynthesizer variable:
bool isStopped;
And this one in VB:
Dim isStopped As Boolean
At the end of your Speak button, set isStopped to false:
C#
isStopped = false;
VB
isStopped = False
For your stop button, set isStopped to true. Add the highlighted line in C#:
if (synthVoice != null)
{
synthVoice.Dispose();
isStopped = true;
}
And this one in VB:
If synthVoice IsNot Nothing Then
synthVoice.Dispose()
isStopped = True
End If
Now adapt your Pause button code. Change it to this in C#:
if (isStopped == false)
{
if (synthVoice.State == SynthesizerState.Speaking)
{
synthVoice.Pause();
}
}
And this in VB:
If isStopped = False Then
If synthVoice.State = SynthesizerState.Speaking Then
synthVoice.Pause()
End If
End If
Do the same for your Resume button. They will only work if isStopped is false. The isStopped Boolean flag is only true when the Stop button is clicked.
The whole of your code should look like this in C# (we've added a null check for the form closing event):
And this in VB Net:
And that's it for the Speech buttons. What we'll do now is get that Open File button working. We want to be able to select three types of file to go into the text box: a Word file, a PDF, and an plain text file. Let's start that in the next lesson.
Back to the Intermediate Programming Contents Page
Email us: enquiry at homeandlearn.co.uk