1

I am making a simple program that has a voice command. I don't exactly know the codes in vb.net so I tried to copy the codes in C# then put it in VB and when I run it, it says "Empty rule is not allowed"

Here's my code:

Private Sub loginCommandFom_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try
        Dim commandChoices As New Recognition.Choices
        Dim grammarBuilder As New Recognition.GrammarBuilder
        Dim gr As New Recognition.Grammar(grammarBuilder)
        commandChoices.Add(New String("Hey", "Wazzup"))
        grammarBuilder.Append(commandChoices)
        commandRecognition.LoadGrammarAsync(gr)
        commandRecognition.SetInputToDefaultAudioDevice()
        commandRecognition.RecognizeAsync()
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Try
End Sub
Private Sub speechCompleted(sender As Object, e As System.Speech.Recognition.RecognizeCompletedEventArgs) Handles commandRecognition.RecognizeCompleted
    commandRecognition.RecognizeAsync()
End Sub
Private Sub speechRecognize(sender As Object, e As System.Speech.Recognition.SpeechRecognizedEventArgs) Handles commandRecognition.SpeechRecognized
    Select Case e.Result.Text
        Case "Hey"
            MsgBox("Yeah")
        Case "Wazzup"
            MsgBox("Yah")
    End Select
End Sub
0

there is small mistake made by you is you are trying to add null grammarbuilder to gramar.

Empty rule is not allowed - here rule means grammar and it is empty at the time when you assign it by using constructor of grammar classGrammar(grammarBuilder)

make following changes.

Private Sub loginCommandFom_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
    Dim commandChoices As New Recognition.Choices
    Dim grammarBuilder As New Recognition.GrammarBuilder
    'removed
    commandChoices.Add(New String("Hey", "Wazzup"))
    grammarBuilder.Append(commandChoices)
    'added
    Dim gr As New Recognition.Grammar(grammarBuilder)
    commandRecognition.LoadGrammarAsync(gr)
    commandRecognition.SetInputToDefaultAudioDevice()
    commandRecognition.RecognizeAsync()
Catch ex As Exception
    MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub
Private Sub speechCompleted(sender As Object, e As System.Speech.Recognition.RecognizeCompletedEventArgs) Handles commandRecognition.RecognizeCompleted
commandRecognition.RecognizeAsync()
End Sub
Private Sub speechRecognize(sender As Object, e As System.Speech.Recognition.SpeechRecognizedEventArgs) Handles commandRecognition.SpeechRecognized
Select Case e.Result.Text
    Case "Hey"
        MsgBox("Yeah")
    Case "Wazzup"
        MsgBox("Yah")
End Select
End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.