Let's talk about it.CodeExcellent solution,
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. speech;
Using system. Speech. synthesis;
Using system. Speech. recognition;
Using system. Globalization;
Using system. Windows. forms;
Namespaces yincheng01@163.com
{
Public class yuyin
{
Public speechrecognitionengine recognizer = NULL;
Public dictationgrammar = NULL;
Public System. Windows. Forms. Control cdisplay;
Public yuyin (string [] FG) // create a key word list
{
cultureinfo myciintl = new cultureinfo ("ZH-CN", false);
foreach (recognizerinfo config in speechrecognitionengine. installedrecognizers () // obtain all voice engines
{< br> If (config. culture. equals (myciintl) & config. id = "MS-2052-80-DESK") // select Chinese
{< br> recognizer = new speechrecognitionengine (config);
break;
}
}< br> If (recognizer! = NULL)
{
Initializespeechrecognitionengine (FG );
Dictationgrammar = new dictationgrammar ();
}
Else
{
MessageBox. Show ("failed to create speech recognition ");
}
}
Private void initializespeechrecognitionengine (string [] FG)
{
Recognizer. setinputtodefaauaudiodevice ();
Grammar customgrammar = createcustomgrammar (FG );
Recognizer. unloadallgrammars ();
Recognizer. loadgrammar (customgrammar );
Recognizer. speechrecognized + = new eventhandler <speechrecognizedeventargs> (recognizer_speechrecognized );
Recognizer. speechhypothesized + = new eventhandler <speechhypothesizedeventargs> (recognizer_speechhypothesized );
}
Public void beginrec (control tbresult) // associate the window control
{
Turnspeechrecognitionon ();
Turndictationon ();
Cdisplay = tbresult;
}
Public void over ()
{
Turnspeechrecognitionoff ();
}
Public Virtual grammar createcustomgrammar (string [] FG)
{
Grammarbuilder = new grammarbuilder ();
Grammarbuilder. append (new choices (FG ));
Return new grammar (grammarbuilder );
}
Private void turnspeechrecognitionon ()
{
If (recognizer! = NULL)
{
Recognizer. recognizeasync (recognizemode. Multiple );
}
Else
{
MessageBox. Show ("failed to create speech recognition ");
}
}
Private void turnspeechrecognitionoff ()
{
If (recognizer! = NULL)
{
Recognizer. recognizeasyncstop ();
Turndictationoff ();
}
Else
{
MessageBox. Show ("failed to create speech recognition ");
}
}
Private void recognizer_speechhypothesized (Object sender, speechhypothesizedeventargs E)
{
}
Private void recognizer_speechrecognized (Object sender, speechrecognizedeventargs E)
{
String text = E. Result. text;
Cdisplay. Text = text;
}
Private void turndictationon ()
{
If (recognizer! = NULL)
{
Recognizer. loadgrammar (dictationgrammar );
}
Else
{
MessageBox. Show ("failed to create speech recognition ");
}
}
Private void turndictationoff ()
{
If (dictationgrammar! = NULL)
{
Recognizer. unloadgrammar (dictationgrammar );
}
Else
{
MessageBox. Show ("failed to create speech recognition ");
}
}
}
}
Class call
String [] fg = {"East", "West", "South", "North "};
Qqq = new yuyin (FG );
Qqq. beginrec (textbox10); controls for displaying recognized information
If you need source code, leave an email.