How to enable voice commands in your Windows Phone 8 App

Microsoft has enhanced the speech capabilities in Windows Phone in the Windows Phone 8 SDK and with the Bing team releasing their API for developers I decided to upgrade my first ever app developed for Windows Phone 7. Also I was getting feedback from the users to update as well as add new features in it. So to start with it I am incorporating voice commands to the app and will explain the steps needed for the same for including in your app too.

Windows phone 8 supports both speech recognition and text-to-speech functionalities for interaction with the users. With the Voice Commands feature, users will be able to instruct the phone to initiate operations that you normally do by touch.

First you need to make sure that you had added the capabilities for speech recognition and microphone in the WpAppManifest.xml either by checking the boxes in the property editor or by manually adding the following lines in the XML file

<Capability Name="ID_CAP_SPEECH_RECOGNITION"/>
<Capability Name="ID_CAP_MICROPHONE"/>

Microsoft has enhanced the speech capabilities in Windows Phone in the Windows Phone 8 SDK and with the Bing team releasing their API for developers I decided to upgrade my first ever app developed for Windows Phone 7. Also I was getting feedback from the users to update as well as add new features in it. So to start with it I am incorporating voice commands to the app and will explain the steps needed for the same for including in your app too.

Windows phone 8 supports both speech recognition and text-to-speech functionalities for interaction with the users. With the Voice Commands feature, users will be able to instruct the phone to initiate operations that you normally do by touch.

First you need to make sure that you had added the capabilities for speech recognition and microphone in the WpAppManifest.xml either by checking the boxes in the property editor or by manually adding the following lines in the XML file

<Capability Name="ID_CAP_SPEECH_RECOGNITION"/>
<Capability Name="ID_CAP_MICROPHONE"/>

 

Add a Voice Command Definition(VCD) file which specifies the various operation that are supported by speech recognition.

 

 

image

 

 

The contents of the file will be as follows

 

xml version="1.0" encoding="utf-8"?>
<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.0">
<CommandSet xml:lang="en-US" Name="MyNotes">
<CommandPrefix>MyNotes CommandPrefix>
<Example> enter a new note >
<Command Name="newNote">
<Example> enter a new note Example>
<ListenFor> Enter [a] [new] note >
<ListenFor> Create [a] [new] note ListenFor>
<Feedback> Creating a new note Feedback>
<Navigate Target="/NewEntry.xaml" >Navigate>
Command> 
CommandSet> >

 

 

The CommandSet tag is used to specify the languages that are supported for speech recognition and in my app I am adding support only for English – US. If you want to add more languages then you need to create another commandset specifying the culture. One restriction in defining commandsets is that you define only one command set per xml:lang value and the name should be unique. Only one command set will active while running the application and it is determined by the setting in the Windows phone

 

 

 

setting

 

 

 

The value specified in the CommandPrefix element  is used to invoke your app and one important thing to remember is that it should be phonetically different from the built in windows phone keywords. The value in example elements will be shown on screen when you start talking as shown below

 

 

speech

 

 

 

The speech recognizer in the Windows Phone will launch the app specified in the Target property under the Navigate element under our commandset block if the utterance by the user matches the entries in the ListenFor elements.

 

 

In the ListenFor element, you can see that some text are wrapped by square brackets which indicates that these words are optional.

The next step is to register the newly added commands in the system grammer and I had done this by calling the the InstallCommandSetsFromFileAsync method in the VoiceCommandService class in the Application_Launching event in App.xaml

 

 

private async void Application_Launching(object sender, LaunchingEventArgs e)
        {
            try
            {
                await VoiceCommandService.InstallCommandSetsFromFileAsync(new Uri("ms-appx:///VoiceCommands.xml"));
            }
            catch (Exception)
            {
                
                throw;
            }
        }

 

And with that we had done with the coding part and let’s see how its working on the emulator.

Just hold the start button until  you hears the listening prompt like the one given below.

 

 

start

 

 

Now I can launch my app using any of the these combination

 

MyNotes Enter a new note

MyNotes Enter Note

MyNotes Create new Note

MyNotes Create a Note

 

 

 

create

 

 

 

This is how you can implement voice commands in your app and in the next part I will be guiding you to implement in-app voice support for your application. Till then happy coding


No Comments

Add a Comment