Public Class Form1 Dim WithEvents RecoContext As SpSharedRecoContext Dim Grammar As ISpeechRecoGrammar 'The Grammar Object so the program knows what is going on. -- Instanced Object: More Info on this later. Dim CharCount As Integer Private Sub Buttonstart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonstart.Click 'The Main Recognition Object Used throughout the whole program. -- Shared Object: More Info on this later. Try If (RecoContext Is Nothing) Then RecoContext = New SpSharedRecoContextClass 'Create a new Reco Context Class Grammar = RecoContext.CreateGrammar(1) 'Setup the Grammar Grammar.Dictationload(1) 'Load the Grammar End If lblstatus.Text = "Recognition Started." Grammar.DictationSetState(SpeechRuleState.SGDSActive) 'Turns on the Recognition. This is Vitally important Buttonstart.Enabled = False Buttonstop.Enabled = True Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Buttonstop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonstop.Click Grammar.DictationSetState(SpeechRuleState.SGDSInactive) 'Turns off the Recognition. It will go dormant. lblstatus.Text = "Recognition Stopped" 'Change the label to let the user know whats up 'Again This is so the user doesn't go breaking things accidently Buttonstart.Enabled = True Buttonstop.Enabled = False End Sub Private Sub OnHypo(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal Result As ISpeechRecoResult) Handles RecoContext.Hypothesis Buttonstop.Enabled = False 'Don't allow the user to stop the recognition until it has completed. 'The button will re-enable in the OnReco Event 'This is so you don't keep printing the same text over and over. It could take up just a tiny bit more processor power 'Its good to not do un-needed things. If lblStatus.Text <> "Receiving" Then lblStatus.Text = "Receiving" End If End Sub 'This sub is fired when the reco engine detects a set of words. This is what you want to use to print or sendkey. 'Use this sub for the final printing of words. Private Sub OnReco(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal RecognitionType As SpeechRecognitionType, ByVal Result As ISpeechRecoResult) Handles RecoContext.Recognition Dim recoResult As String = Result.PhraseInfo.GetText 'Create a new string, and assign the recognized text to it. 'This block will print to the textbox built into the program 'If you would prefer to use the SendKeys method, Comment out this entire block. And Uncomment the SendKeys Line. txtBox.Selectionstart = CharCount txtBox.SelectedText = recoResult & " " CharCount = CharCount + 1 + Len(recoResult) 'Uncomment the next line if you want to send the text to the selected window rather than constrain it to the textbox. 'SendKeys.Send(recoResult & " ") 'This line sends the result via SendKeys to the top window. lblStatus.Text = "Finished Dictating" Buttonstop.Enabled = True End Sub
Just like one of the responders, I am getting a result of
EXCEPTION FROM HRESULT: 0X8004503A when I try to load the grammar file.
Debugging just showed me a little more detail, but not enough to solve the problem:
Error is coming from Grammar.RecoContext.Recognizer.AudioInputStream
when I try to execute this line:
I am usiing XP and Visual Studio 2008 and SAPI 5.1.
I have been struggling with this for almost a month.
Please advise ASAP. Thanks in advance for your kind attention.