Visual Basic .Net语音命令
问题描述:
我正在制作一个具有语音命令的简单程序。我不完全知道vb.net中的代码,所以我试图复制C#中的代码,然后把它放在VB中,当我运行它时,它说“空规则是不允许的”Visual Basic .Net语音命令
这是我的代码:
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
答
有你犯的一个小错误是你正在尝试将null grammarbuilder添加到gramar中。
空规则是不允许的 - 在这里规则意味着语法,当您使用语法classGrammar(grammarBuilder)的构造
化妆以下更改为它分配它在当时是空的。
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