Just a few steps to make your WP app recognizes your speech


Do you want to add a functionality in your Windows Phone device which can actually listen to users voice or commands? Do you want the app to record the speech or convert the speech into a text string which you can save as a document?

 

Okay, this blog post will cover that topic for you today. Here we will discuss how to use the API to enable the speech recognition in your WP device.

 

The sealed class named “SpeechRecognizerUI”, comes under the namespace “Windows.Phone.Speech.Recognition” enables your Windows Phone device to enable speech recognition functionality. You can control whether to display a graphical user interface or not by modifying few properties.

 

The class provides a property named “Settings” of type “SpeechRecognizerUISettings”, which you can modify to show or hide the following items during speech recognition:

    • ExampleText - The example text that is shown on the Listening page.
    • ListenText - The heading text that displays on the Listening page, above the example text.
    • ReadoutEnabled - Indicates if the successfully recognized text will be spoken back to the user on the confirmation page.
    • ShowConfirmation - Indicates if a confirmation page is shown to the user after speech recognition is successful.

Speech Recognition with UI

The method “RecognizeWithUIAsync()” begins a speech recognition session for a SpeechRecognizerUI object and returns the result of the speech recognition session that was initiated by that SpeechRecognizerUI object. If you want to show the default speech recognition UI in the screen, call this method which will show in the screen as shown below:

 

Windows Phone 8 "SpeechRecognizerUI" started listening to users voiceWindows Phone 8 "SpeechRecognizerUI" started recognizing the users voiceWindows Phone 8 "SpeechRecognizerUI" returns the text recognized from users voice

 

The first screenshot is where the phone device will start listening to the users speech, then it starts recognizing the voice and returns an equivalent text string to the caller which we displayed by calling the MessageBox.Show() method. By default, it uses the current grammar settings file from the device.

 

Here is the code snippet of what we demonstrated above:

 

private async void StartListening()
{
// Create an instance of SpeechRecognizerUI.
var speechRecognizerUI = new SpeechRecognizerUI();
speechRecognizerUI.Settings.ReadoutEnabled = false;
speechRecognizerUI.Settings.ShowConfirmation = false;

// Start recognition (load the dictation grammar by default).
var recoResult = await speechRecognizerUI.RecognizeWithUIAsync();
MessageBox.Show(recoResult.RecognitionResult.Text);
}

 

If you set “speechRecognizerUI.Settings.ShowConfirmation = true” above, you will see a confirmation screen like the first image shown below. In case it misses to recognize, it shows a message “Sorry, didn’t catch that” in the screen as shown in the second image below:

 

wp_ss_20131003_0004wp_ss_20131003_0005

 

Speech Recognition without UI

In case you don’t want to show any of the above UI in the screen and want to go with your own custom UI (like the one shown here. Did you recognize the character of the girl here?) to recognize users voice/speech and convert them to a recorded message to play or a text message string to save as a document file, then you need to use some other API call of the SpeechRecognizerUI class.

 

Call to “speechRecognizerUI.Recognizer.RecognizeAsync()” asynchronous method will help you to recognize and capture voice silently, without showing the Speech Recognition UI. The method returns the result of the speech recognition session that was initiated by the SpeechRecognizer object.

 

Here is the code snippet of the same:

 

private async void StartListening()
{
// Create an instance of SpeechRecognizerUI.
var speechRecognizerUI = new SpeechRecognizerUI();
speechRecognizerUI.Settings.ReadoutEnabled = false;
speechRecognizerUI.Settings.ShowConfirmation = false;

// Start recognition (load the dictation grammar by default).
var recoResult = speechRecognizerUI.Recognizer.RecognizeAsync().GetResults();
MessageBox.Show(recoResult.Text);
}

 

I hope the post was easy to read and helped you to learn how to capture speech recognition in Windows Phone 8 devices by writing a small piece of code. Now you will be able to use that quickly and easily in your phone applications. Check out the other properties like Grammars etc. part of the API.

 

Please drop a line below if you have any questions. I am also available on Twitter and Facebook. So, connect with me for any queries and to get regular technical and/or article updates.


7 comments

  1. Is this code works fine when application in background and device can listen speech.

    ReplyDelete
    Replies
    1. Not yet tried. Let me check that. BTW, what was the issue in your case?

      Delete
  2. Issue: if some speak like "Help" whether app in foreground and background then some code of app is execueted. Above code is working fine when app is in foreground then speak "Help" then functionality executed. but not in case of background app.

    ReplyDelete
  3. when this line "var recoResult = speechRecognizerUI.Recognizer.RecognizeAsync().GetResults();" is executed then error shows like "a method was called at an unexpected time".

    any suggestions are welcome.

    ReplyDelete
    Replies
    1. Looks like you called the API either in constructor or in the loaded event of the page. This will not work there. Place a button and register a tap event. Inside the event, call the API. It will work.

      Delete
  4. I used this code var res = _speechRecognizer.RecognizeAsync();
    var result = await Task.WhenAny(res.AsTask());

    works fine in constructor.

    ReplyDelete


 
© 2008-2014 Kunal-Chowdhury.com | Designed by Kunal Chowdhury
Back to top