听写语音在Visual Studio中的SAPI下不起作用

时间:2018-06-21 20:26:02

标签: speech-recognition sapi

使用Visual Studio 2012框架4.5 安装并加载Microsoft Speech(Speechlib)sdk11,运行时和语音包 将“ Microsoft语音对象库(v11)添加到我的引用中。 我还在项目中进行了“清理”,并尝试也使用SAPI 5.4。 VS给出:

无论输入的参数是什么,或者如果我不使用任何参数(m_Grammar.Dictationload()),则grammar.dictationload()中的“ ArgumentException”的值都不会在可接受的范围内

Imports SpeechLib

Public Class Form1
    Public WithEvents m_Recocontext As SpInProcRecoContext ' Create an in process recognizer context (not a shared one!)
    Public m_Recognizer As SpInprocRecognizer              ' Create an in process recognizer
    Public m_Grammar As ISpeechRecoGrammar                 ' Create a Grammar to load up with our command words

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    m_Recocontext = New SpInProcRecoContext         ' Create an instance of a Recognizer from a Reco Context
    m_Grammar = m_Recocontext.CreateGrammar(0)      ' Can create multiple grammars, set to default of 0
    m_Recognizer = m_Recocontext.Recognizer         ' Create a recognizer to listen for speech

    ' Create a dictation grammar and set it inactive
    m_Grammar.DictationLoad("", SpeechLoadOption.SLODynamic) '*** EXCEPTION HERE ***
    m_Grammar.DictationSetState(SpeechRuleState.SGDSInactive)

    ' Create a command and control grammar and set it inactive
    m_Grammar.CmdLoadFromFile(Application.StartupPath & "\grammartest.xml", SpeechLoadOption.SLODynamic)
    m_Grammar.CmdSetRuleIdState(1, SpeechRuleState.SGDSInactive) 
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    m_AudioIn.DeviceId = 0 ' default wave in device
    m_AudioIn.Format.Type = SpeechAudioFormatType.SAFT8kHz16BitMono
    m_Recognizer.AllowAudioInputChangesOnNextSet = False ' *** EXCEPTION thrown here ***
    m_Recognizer.AudioInputStream = m_AudioIn
    m_Grammar.DictationSetState(SpeechRuleState.SGDSActive)    ' listen in dictation mode
End Sub

Private Sub OnRecognition(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, _
                              ByVal RecognitionType As SpeechRecognitionType, ByVal Result As ISpeechRecoResult) Handles m_Recocontext.Recognition
        Debug.Print("OnRecognition, Text: " & Result.PhraseInfo.GetText) 'xxx testing. remove when done
End Sub
End Class

如果我在该行中添加注释,则在下一行中会收到一个COM异常,并显示80045003错误,我看到有很多人对此一无所获。 (以及在AllowAudioChangesOnNextSet行中)。数小时的Google搜索唯一可能的想法是,SpeechLib中存在一个错误,并且某些函数处于错误的线程中,因此您必须在单独的MTA线程中运行它们。无论如何,在线上有许多此代码示例必须对人有用,而VS用intellisense为我完成了该代码,但它不起作用。相同的代码在VB6中有效。并且命令和控制在VS中工作正常。我试图添加听写,这是行不通的。

0 个答案:

没有答案