A very interesting feature of Windows 7 is the API for recognizing voice commands. With this technology we can easily create applications that execute commands from predefined words.

As an introduction to this technology will create an application that understands two commands: “Music” and “Yes”.

In this tutorial I have a WPF project, and as a first step I need to add reference to dll System.Speech.Recognition.

Then I create two variables:

SpeechRecognizer speechReco = new SpeechRecognizer();
List<string> grammerList = new List<string>();

Now, we initialize my voice recognition system. Thus:

public MainWindow()
{
    InitializeComponent();

    grammerList.AddRange(new string[] { "music", "yes" });

    Choices myChoices = new Choices(grammerList.ToArray());
    GrammarBuilder builder = new GrammarBuilder(myChoices);
    Grammar gram = new Grammar(builder);
    speechReco.LoadGrammar(gram);
    speechReco.Enabled = true;
    speechReco.SpeechRecognized += new EventHandler<speechrecognizedeventargs>(speechReco_SpeechRecognized);
}

In the above, we use the API for speech recognition to listen to the commands “music” and “yes”. And when one of these commands is captured, our method speechReco_SpeechRecognized runs.

So you can see the commands being executed. Use this code:

void speechReco_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    if (e.Result.Text == "music")
    {
        ImageBrush imgBrush = new ImageBrush();
        imgBrush.ImageSource = new BitmapImage(new Uri(@"D:\wedding.png"));

        this.Background = imgBrush;
    }

    if (e.Result.Text == "yes")
    {
        ImageBrush imgBrush = new ImageBrush();
        imgBrush.ImageSource = new BitmapImage(new Uri(@"D:\yes-verde.png"));

        this.Background = imgBrush;
    }
}

To illustrate the above code, watch the video: