A c #3.0-based general class developed based on Vista Speech Recognition

Source: Internet
Author: User

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.

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.