I have been away from programming for many years and I am trying to develop a simple speech recognition app in visual basic. Here is the problem that I am experiencing:
Com Exception was unhandled:
Exception from HRESULT: 0x80045048
My code:
Imports System.Xml
'Custom Imports
Imports SpeechLib
Public Class Form1
'Declares
Dim WithEvents RecoContext As SpSharedRecoContext 'The Main Recognition Object Used throughout the whole program. -- Shared Object: More Info on this later.
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 'This is used to count the amount of chars that are in the text box.
Public Const SGDSActive As SpeechLib.SpeechRuleState = 0
Public Const SGDSInactive As SpeechLib.SpeechRuleState = 1
Public Const SLOStatic As SpeechLib.SpeechLoadOption = 1
'Start Button. This will engage reco, and start the entire process.
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
'First check to see if reco has been loaded before,if not load it.
If (RecoContext Is Nothing) Then
RecoContext = New SpSharedRecoContextClass 'Create a new Reco Context Class
Grammar = RecoContext.CreateGrammar(1) 'Setup the Grammar
Grammar.CmdLoadFromFile("E:\myGrammar.XML", SpeechLoadOption.SLOStatic) 'Load the Grammar
End If
lblStatus.Text = "Recognition Started" 'Change the Label to let the user know whats up
Grammar = RecoContext.CreateGrammar
Grammar.CmdLoadFromFile("myGrammar.XML", SLOStatic)
Grammar.CmdSetRuleIdState(101, SGDSActive) 'Turns on the Recognition. This is Vitally important
'This is so the user doesn't break the program by
'trying to start the recognition after its already started.
btnStart.Enabled = False
btnStop.Enabled = True
End Sub
''''
'Stop Button. This will stop the recoginition
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.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
btnStart.Enabled = True
btnStop.Enabled = False
End Sub
''''
'This is the hypothesis sub. The hypothesis is not the final recognition. This will fire many times per word. You do not want to print anything that is final from the hypothesis.
'This is not required for the final recognition. But it is vital to understand it.
Private Sub OnHypo(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal Result As ISpeechRecoResult) Handles RecoContext.Hypothesis
btnStop.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.
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"
btnStop.Enabled = True
End Sub
End Class
Mod Edit: Please use code tags when posting your code. Code tags are used like so =>
Thanks,
PsychoCoder

New Topic/Question
Reply




MultiQuote




|