Option Strict On
Imports System.Speech
Imports System.Speech.Recognition
Imports System.Speech.Recognition.SrgsGrammar
Public Class Form1
Dim WithEvents reco As New SpeechRecognitionEngine
Dim synth As New Synthesis.SpeechSynthesizer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim gram As New SrgsDocument
Dim colorRule As New SrgsRule("color")
Dim colorsList As New SrgsOneOf("red", "green")
colorRule.Add(colorsList)
gram.Rules.Add(colorRule)
gram.Root = colorRule
reco.LoadGrammarAsync(New Recognition.Grammar(gram))
reco.SetInputToDefaultAudioDevice()
reco.RecognizeAsync(RecognizeMode.Multiple)
Catch s As Exception
MessageBox.Show(s.Message)
End Try
End Sub
Private Sub reco_SpeechDetected(ByVal sender As Object, ByVal e As System.Speech.Recognition.SpeechDetectedEventArgs) Handles reco.SpeechDetected
Label1.Text = "Voz detectada"
End Sub
Private Sub reco_SpeechRecognized(ByVal sender As Object, ByVal e As System.Speech.Recognition.RecognitionEventArgs) Handles reco.SpeechRecognized
Select Case e.Result.Text
Case "vermelho"
SetColor(Color.Red)
Case "verde"
SetColor(Color.Lime)
End Select
End Sub
Private Sub SetColor(ByVal color As System.Drawing.Color)
reco.RecognizeAsyncCancel()
reco.RecognizeAsyncStop()
synth.Speak("mudando cor para " & color.ToString)
Me.BackColor = color
reco.RecognizeAsync(RecognizeMode.Multiple)
End Sub
End Class
P.S。:我用葡萄牙语运行Windows 7,有什么问题吗?