Tag Archives: voice recognition

Tips and tricks when using voice recognition in a Windows Phone 8 app

While working (again) on 2Day, I encountered small issues with voice recognition. The next version of 2Day will bring support for both speech recognition (use your voice to set the title of a task) and voice commands (speak to your phone to perform operations, for example: “2Day add a reminder tomorrow at 9PM”).

While developing those new features I used two distinct API for voice recognition:

The first one provides no UI while the second one includes a default GUI which is uniform across apps and utilities:

Listenning

Issue 1: stop music playback automatically

In 2Day, I use the SpeechRecognizerUI when the user must confirm a choice while seeing what is visible on the screen. When using those APIs I faced a first problem which is music playback is not stopped automatically during voice recognition. It turns out a simple API can be used for this:

public void PauseMusicPlayback()
{
    this.wasPlayingMusic = MediaPlayer.State == MediaState.Playing;
    if (this.wasPlayingMusic)
    {
        FrameworkDispatcher.Update();
        MediaPlayer.Pause();
        FrameworkDispatcher.Update();
    }            
}

public void ResumeMusicPlayback()
{
    if (this.wasPlayingMusic)
    {
        // it looks like FrameworkDispatcher.Update() is needed when using the MediaPlayer
        FrameworkDispatcher.Update();
        MediaPlayer.Resume();
        FrameworkDispatcher.Update();
    }
}

With a simple boolean field, I can save before starting voice recognition if I will have to resume music playback.

Issue 2: use default sound with SpeechRecognizer

When using the SpeechRecognizer class (the one without the default UI) I faced another problems: start/end of voice recognition is not surrounded by the small system sound that indicates that the recognition has started/ended. I thought the easiest way to get this sound as a WAV file on my machine was to use an Audio Capturing tool such as Audacity.

Long story short: that was a bad idea. And it turns out… The original sound files are installed with the SDK !

If you browse the following directory C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v8.0\Sounds you will be able to get the 2 WAV files ! And use them in your app (you can see my older blog post about how to play WAV file in a Silverlight Windows Phone app).

SoundFiles

Hope it helps 🙂