Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Using voice commands in VB

I want to be able to use voice commands for when a button is clicked.
For example, a button called 'Address' speaks out 'Address' back to the user. Any idea how to go about doing this?
[214 byte] By [MonaVohra] at [2007-11-11 10:20:29]
# 1 Re: Using voice commands in VB
If you install Microsoft's Speech SDK ( http://www.microsoft.com/downloads/details.aspx?FamilyId=5E86EC97-40A7-453F-B0EE-6583171B4530&displaylang=en), you can do this: http://discuss.fogcreek.com/dotnetquestions/default.asp?cmd=show&ixPost=6566
Phil Weber at 2007-11-11 20:48:12 >
# 2 Re: Using voice commands in VB
cheers...will have a go at that :)
MonaVohra at 2007-11-11 20:49:13 >
# 3 Re: Using voice commands in VB
I input the code:

Private Sub Btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn1.Click
Try
Dim x As String = Me.TxtBox1.Text
Dim voice As New SpVoice
voice.Speak(x, SpeechVoiceSpeakFlags.SVSFlagsAsync)
Catch ex As Exception
MsgBox("Your PC cannot play the sound because:" & vbCrLf _
& "(1) No speech recognition engine Is installed On this PC, Or" _
& vbCrLf & "(2) This PC has no sound card", MsgBoxStyle.Critical + _
MsgBoxStyle.OKOnly, "Critical Error")
End Try
End Sub

as given on the website http://discuss.fogcreek.com/dotnetq...how&ixPost=6566.

Gives two errors:

1) Type 'SpVoice' is not defined

2) Name 'SpeechVoiceSpeakFlags' is not declared
MonaVohra at 2007-11-11 20:50:14 >
# 4 Re: Using voice commands in VB
In your project, make sure you add a reference to the speech engine in the projects properties dialog.

From the article:

First, import the COM Object as a reference made to the Microsoft Speech Object Library, then add the following code above the Public CLASS Line of the form's code window:

Imports SpeechLib
joewmaki at 2007-11-11 20:51:15 >
# 5 Re: Using voice commands in VB
Joe,

I have put:

Imports speechlib as shown in the website http://discuss.fogcreek.com/dotnetq...how&ixPost=6566.

does not work...
MonaVohra at 2007-11-11 20:52:10 >
# 6 Re: Using voice commands in VB
You also need to add the Microsoft Speech Object Library to your project references. You can find it under the COM tab of the Add Reference dialog (assuming you ran the setup of the sdk). I tried it and the code example works fine on my machine.
joewmaki at 2007-11-11 20:53:10 >
# 7 Re: Using voice commands in VB
Hi,

I have added the Microsoft Speech Object Library to your project references. Then i added Imports SpeechLib in my code. And then I pasted the code that i need from http://discuss.fogcreek.com/dotnetq...how&ixPost=6566

I have no errors with the code. But how do i use the voice commands now?
I have for example, an interface with two buttons. These are:

Plan Journey
Help

When I click on Plan Journey button, I want it to say Plan Journey back to me. How does this work? Do i need to read this up somewhere?
MonaVohra at 2007-11-11 20:54:21 >
# 8 Re: Using voice commands in VB
Notice that the sample code on the Fog Creek site is reading the text from a textbox:

Dim x As String = Me.TxtBox1.Text
Dim voice As New SpVoice
voice.Speak(x, SpeechVoiceSpeakFlags.SVSFlagsAsync)

You want it to read the caption of a button. So instead of passing the .Text property of a textbox to the voice.Speak method, you want to pass the .Text property of the button that triggered the Click event. If the button is named "btnPlan", your code would look like this:

Private Sub btnPlan_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnPlan.Click

' sender parameter is the control that triggered
' the Click event. Convert it to a Button:
Dim btn As Button = DirectCast(sender, Button)

' Now pass the button's Text property to the
' voice.Speak method:
Dim voice As New SpVoice
voice.Speak(btn.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync)

End Sub
Phil Weber at 2007-11-11 20:55:20 >
# 9 Re: Using voice commands in VB
is there anything else that needs to be done after entering this code?
MonaVohra at 2007-11-11 20:56:14 >
# 10 Re: Using voice commands in VB
make sure your speakers are on :D

If you use a generic routine like this:

Sub Speak(ByVal strSpeak As String)
Try
Dim voice As New SpVoice
voice.Speak(strSpeak, SpeechVoiceSpeakFlags.SVSFlagsAsync)
voice = Nothing
Catch ex As Exception
MessageBox.Show(ex.Message, "Speak Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

You just need to call: Speak("What Ever String You Want Read")
joewmaki at 2007-11-11 20:57:16 >
# 11 Re: Using voice commands in VB
speakers are on...full blast :D

still no sound. iv jus copied the code u sent aswell. now i'm totally confused!
MonaVohra at 2007-11-11 20:58:16 >
# 12 Re: Using voice commands in VB
speakers are on...full blast :D

still no sound. iv jus copied the code u sent aswell. now i'm totally confused!

Well, the problems not with the code. It may be the install of the speech sdk or how the speech engine is configured. I have as much experience with that as you do. :confused:
joewmaki at 2007-11-11 20:59:26 >
# 13 Re: Using voice commands in VB
is it?? ok...thanks for your help tho. very much appreciated :D
MonaVohra at 2007-11-11 21:00:23 >
# 14 Re: Using voice commands in VB
You just need to call: Speak("What Ever String You Want Read")

where is this in the code??
MonaVohra at 2007-11-11 21:01:23 >
# 15 Re: Using voice commands in VB
no worries iv got it to work. thanks guys ::D
MonaVohra at 2007-11-11 21:02:21 >
# 16 Re: Using voice commands in VB
by the way...would u know why this problem is occuring...

i click on a button and it gives a voice command back to me. then when i click it again a minute later by going back to the form the voice command is not given for a button that did work before.
MonaVohra at 2007-11-11 21:03:22 >
# 17 Re: Using voice commands in VB
by the way...would u know why this problem is occuring...

i click on a button and it gives a voice command back to me. then when i click it again a minute later by going back to the form the voice command is not given for a button that did work before.

It works correctly here, can you post the code you have in the click event of the button?
joewmaki at 2007-11-11 21:04:25 >
# 18 Re: Using voice commands in VB
Public Class JourneyPlanner

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Street.Show()
Me.Hide()

' sender parameter is the control that triggered
' the Click event. Convert it to a Button:
Dim btn As Button = DirectCast(sender, Button)

' Now pass the button's Text property to the
' voice.Speak method:
Dim voice As New SpVoice
voice.Speak(btn.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync)

End Sub

That is for the button which is called 'Street'

Could it be because I am hiding the form instead of closing it? Not sure
MonaVohra at 2007-11-11 21:05:25 >
# 19 Re: Using voice commands in VB
Could it be because I am hiding the form instead of closing it? Not sure

Not sure. Let's see if I have this straight.

Your showing Street and then hiding the current form and voice.speak works

Your coming back to the original form and clicking the button again (after it's unhidden) and the speak doesn't work. Correct?

Does it still show street and hide itself? If not the click event may not be firing. If so, I don't have a good suggestion. You might try moving the Speak method before the .Show/.Hide. But I'm just shooting in the dark :D
joewmaki at 2007-11-11 21:06:31 >
# 20 Re: Using voice commands in VB
ok let me give you an example of how i have got my forms:

Form 1 is called Main Screen. It has the button:
Plan Journey

When I click on Plan Journey it takes me to Form 2 called Plan Journey. This form has this button:
Journey Planner

When I click on Journey Planner it takes me to Form 3 called Journey Planner. This form has this button:
Street

Each of the three buttons has a Back button. When this is clicked it takes u back to the previous form.

So if I click on Plan Journey button, it speaks back Plan Journey to me and it opens the Journey Planner form. The Plan Journey then hides. If I click the Back button, it speaks Back to me and I am taken to the previous form. The current form is again hidden.

I keep doin this between form1 and form2 and it perfectly speaks to me Plan Journey and Journey Planner. But when I go to and click the Street button, it does not play Street to me. Sometimes it does. Sometimes it doesnt.

I have tried what you suggested. Placing the speak method before the .show/.hide. But that also does not work :(
MonaVohra at 2007-11-11 21:07:30 >
# 21 Re: Using voice commands in VB
But when I go to and click the Street button, it does not play Street to me. Sometimes it does. Sometimes it doesnt.(

Does it still show the Street form? If so the event is running as expected, just the speak method is at issue, correct? If this is the case I'm at a loss to explain it (sorry). Ouside of placing a breakpoint in your event code and stepping through it to make sure al the variables are getting passed correctly, I would not know what to suggest.
joewmaki at 2007-11-11 21:08:27 >