Click here to Skip to main content
15,994,910 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have all speech items needed as the project will run with voice commands and respond the way it should. this is a small piece of the code. when i had this assigned to a button on the form it would play one ramdom wav, they are only about 10 seconds long at the most. i want to move to when i say one of the key words it will respond the same as when you push the button. i think i need one line of code but can't figure it out.


I'm working on a speech req program and I'm trying to get it to play a random .wav file when i say a key word. this is what i have so far and it does work if you push a button on the form but i want it to play when it hears one of the words spoken. but can't get the last peace, any help would be great. still learning:


C#
case "for":
case "help":
case"now":
    string[] files = Directory.GetFiles(@"C:\Users\chuck\voice");
    Random rnd = new Random(Guid.NewGuid().GetHashCode());
    int choice = rnd.Next(0, files.Length - 1);
    string soundFile = files[choice];
    System.Media.SoundPlayer player = new System.Media.SoundPlayer(soundFile);
    player.Play();



I'm doing this in C#
Posted
Updated 21-May-14 8:00am
v3
Comments
DamithSL 20-May-14 22:15pm    
any exceptions? what is not working?
Bernhard Hiller 21-May-14 2:31am    
Also note that this will require a "full-duplex" recording device (can record and playback at the same time) - not all dictation devices can do that!

1 solution

You can use System.Speech.Recognition:
http://msdn.microsoft.com/en-us/library/system.speech.recognition%28v=vs.110%29.aspx[^].

The alternatives are: SpeechRecognizer or SpeechRecognitionEngine:
http://msdn.microsoft.com/en-us/library/system.speech.recognition.speechrecognizer(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.speech.recognition.speechrecognitionengine(v=vs.110).aspx[^].

First one requites STA thread apartment state, and second one MTA. So, if you use WPF which requires STA, you can work SpeechRecognitionEngine in a separate thread set to MTA; and other application types could use any apartment state in the main threads. This is the usual difficulty, because this subtlety is not documented by Microsoft properly; everything else is more or less straightforward. And, well… quality of recognition of big grammar sets is quite questionable, but with just a set of few commands it's good enough to make it doable.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900