With the. NET Speech recognition Library, it's easy to make simple question-and-answer programs like Siri.
The implementation steps are as follows:
1. Create a new project and add a reference to the System.speech. (as shown)
2. Define speech recognition (speechrecognitionengine) and voice reading (SpeechSynthesizer) separately in the window
Speechrecognitionengine _recognizer = new Speechrecognitionengine (); SpeechSynthesizer Siri = new SpeechSynthesizer ();
3. Add the following code to the form launch:
CultureInfo Myciintl = new CultureInfo ("ZH-CN"); foreach (Recognizerinfo config in speechrecognitionengine.installedrecognizers ()) {if (config.c Ulture. Equals (myciintl) && config. Id = = "Ms-2052-80-desk") {_recognizer = new speechrecognitionengine (config); Break }} _recognizer. Setinputtodefaultaudiodevice (); _recognizer. Loadgrammar (New Grammar new Grammarbuilder (New Choices (File.ReadAllLines (Application.startuppath + "\\command.txt") )))); _recognizer. speechrecognized + = new eventhandler<speechrecognizedeventargs> (_recognizer_speechrecognized); _recognizer. Recognizeasync (recognizemode.multiple);
void _recognizer_speechrecognized (object sender, Speechrecognizedeventargs e) {string speech = E.result.text; Switch (speech) {case "Hello": Siri.speak ("Hello"); Break Case "Today's Day of the Week": Siri.speak ("Today" + DateTime.Today.ToString ("dddd")); Break Case "Date Today": Siri.speak ("Today is" + DateTime.Today.ToString ("Yyyy-mm-dd")); Break Case "What's The Time": DateTime now = DateTime.Now; String time = Now. Getdatetimeformats (' t ') [0]; Siri.speak (time); Break Case "Shutdown": System.Diagnostics.Process.Start ("Shutdown", "-S"); Break } }
All commands are saved in the "Command.txt" file, remember to use Unicode to save the line.
Run the program and you can implement the Voice quiz.
The program can also be applied to the use of voice control home appliances, the realization of smart Home (reference http://blog.csdn.net/soft2buy/article/details/6248333).
Complete code download for this article:
C # Create a simple app like Siri's voice quiz