System.speech dll错误

时间:2016-07-14 10:49:24

标签: vb.net speech-recognition

好的,所以我对这种编程类型(使用语音识别)完全陌生,尽管我想学习,但我试图搜索一些示例代码,我偶然发现了这一点:

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.Blue
        ElseIf (e.Result.Text = "No") Then
            LabelNo.ForeColor = Color.Blue
        ElseIf (e.Result.Text = "Maybe") Then
            LabelMaybe.ForeColor = Color.Blue
        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

事情是,它似乎没问题。虽然我有一个破碎的麦克风(我之所以无法测试它是否正常工作),但是直接的窗口让我这样做:

A first chance exception of type 'System.InvalidOperationException' occurred in System.Speech.dll
A first chance exception of type 'System.NullReferenceException' occurred in System.Speech.dll

知道那些是什么?我错过了一个dll吗?还是有什么东西我需要继续这个?提前谢谢,更有力量!

0 个答案:

没有答案