C# 如何使用语音识别引擎而不是默认的windows弹出式识别器使此代码正常工作
我还在做我的语音测试。我有这个代码,但我需要它来使用C# 如何使用语音识别引擎而不是默认的windows弹出式识别器使此代码正常工作,c#,vb.net,speech-recognition,C#,Vb.net,Speech Recognition,我还在做我的语音测试。我有这个代码,但我需要它来使用SpeechRecognitionEngine而不是系统弹出式识别器。这段代码是用VB编写的,尽管我更喜欢用C#编写,但这太难了,我只能接受我能得到的任何代码或编程语言。这段代码完全是功能性的,可以通过简单地粘贴到一个空的VB项目中进行测试,包括导入System.Speech.Recognition。有人能告诉我如何让这段代码使用SpeechEngine和 我可以讨论设置默认的音频输入和删除识别器的启动代码,当然必须删除它。但是,为了简单起见,
SpeechRecognitionEngine
而不是系统弹出式识别器。这段代码是用VB编写的,尽管我更喜欢用C#编写,但这太难了,我只能接受我能得到的任何代码或编程语言。这段代码完全是功能性的,可以通过简单地粘贴到一个空的VB项目中进行测试,包括导入System.Speech.Recognition
。有人能告诉我如何让这段代码使用SpeechEngine和
我可以讨论设置默认的音频输入和删除识别器的启动代码,当然必须删除它。但是,为了简单起见,我将代码保留为实时测试。谢谢
Imports System.Speech.Recognition
Imports System.Threading
Imports System.Globalization
Public Class Form1
' recogniser & grammar
Dim recog As New SpeechRecognizer
Dim gram As Grammar
' events
Public Event SpeechRecognized As _
EventHandler(Of SpeechRecognizedEventArgs)
Public Event SpeechRecognitionRejected As _
EventHandler(Of SpeechRecognitionRejectedEventArgs)
' word list
Dim wordlist As String() = New String() {"Yes", "No", "Maybe"}
' word recognised event
Public Sub recevent(ByVal sender As System.Object,
ByVal e As RecognitionEventArgs)
LabelYes.ForeColor = Color.LightGray
LabelNo.ForeColor = Color.LightGray
LabelMaybe.ForeColor = Color.LightGray
If (e.Result.Text = "Yes") Then
LabelYes.ForeColor = Color.Green
ElseIf (e.Result.Text = "No") Then
LabelNo.ForeColor = Color.Green
ElseIf (e.Result.Text = "Maybe") Then
LabelMaybe.ForeColor = Color.Green
End If
End Sub
' recognition failed event
Public Sub recfailevent(ByVal sender As System.Object,
ByVal e As RecognitionEventArgs)
LabelYes.ForeColor = Color.LightGray
LabelNo.ForeColor = Color.LightGray
LabelMaybe.ForeColor = Color.LightGray
End Sub
' form initialisation
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
' need these to get British English rather than default US
Thread.CurrentThread.CurrentCulture = New CultureInfo("en-GB")
Thread.CurrentThread.CurrentUICulture = New CultureInfo("en-GB")
' convert the word list into a grammar
Dim words As New Choices(wordlist)
gram = New Grammar(New GrammarBuilder(words))
recog.LoadGrammar(gram)
' add handlers for the recognition events
AddHandler recog.SpeechRecognized, AddressOf Me.recevent
AddHandler recog.SpeechRecognitionRejected, AddressOf Me.recfailevent
' enable the recogniser
recog.Enabled = True
End Sub
End Class