(Difference between revisions)

Voice Commands in Windows Phone 8

From Nokia Developer Wiki
Jump to: navigation, search
mehul_raje (Talk | contribs)
m (Mehul raje - - Modify Voice Command Definition file)
mehul_raje (Talk | contribs)
m (Mehul raje - - Sample Voice Commands)
Line 279: Line 279:
==Sample Voice Commands==
==Sample Voice Commands==
Following is list of some commands which user can invoke using above VCD file, make sure that before invoking below voice commands the voice commands from VCD file must be install/register with OS. <br />
To start recognizer hold start key from within the application or from outside the application.
# hello what is current date
# hello what is current date
# hello what is current battery percentage.
# hello what is current battery percentage.

Revision as of 16:20, 19 November 2012

This article explains how to use voice commands in an application to perform actions, tasks or any initiative work in Windows Phone 8


WP Metro Icon Multimedia.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
Article Metadata
Code ExampleTested with
SDK: Windows Phone SDK 8.0
Devices(s): Windows Phone 8 emulator
Platform(s): Windows Phone 8 and later
Windows Phone 8
Platform Security
Created: mehul_raje (06 Nov 2012)
Last edited: mehul_raje (19 Nov 2012)



Windows phone 8 comes with very great speech functionality which includes:

  1. voice commands
  2. speech recognition



Refer following screen shots

Basic voice command listening UI Basic what can i say UI
Listening.png Whatcanisay.png

How to achieve

The following steps need to be performed in order to add voice command functionality in an application:

  1. Create voice command file
  2. Install voice command file in OS
  3. Write code to handle navigation handling and command execution.

Capabilities required


To add capabilities in your project

  1. Go to your project from Solution Explorer and open WMAppManifest.xml file from Properties section.
  2. Click Capabilities tab and check above mentioned capabilities and save the file.

Create voice command definition file

The following steps describe how to create Voice command definition file:

  1. Right click the windows phone project in visual studio
  2. Go to Add New Item
  3. Select Voice Command Definition from list
  4. Give proper name to the VCD file.

Refer following screen shot.

Add new VCD file in project

By default visual studio provides basic template of VCD file which you need to update with your own voice commands and their intended actions. Typical voice command file contains

  1. Example phrase to show how user can invoke command

Voice Command Definition file in detail

Let us consider the following example of VCD file:

<Example>hello what is current date</Example>
<Command Name="Util">
<Example> What is current time </Example>
<Feedback>showing current {options}</Feedback>
<Navigate Target="Target.xaml"/>
<Command Name="Calculator">
<Example> 2 plus 3 </Example>
<Navigate Target="Target.xaml"/>
<PhraseList Label="options">
<Item>battery percentage</Item>
<PhraseList Label="operation">
<PhraseList Label="operand1">
<PhraseList Label="operand2">

VoiceCommands: This is a root element of voice command definition file which can contain one or more CommandSet elements each of which contains voice commands.

CommandSet: This element serves as container for all voice command. User can define actual voice commands under it for a single language defined for xml:lang attribute of CommandSet. User can also specify name for CommandSet element but it is optional. But it becomes useful when user wants to modify PhraseList pragmatically as you can see this in Modify Voice Command Definition file section of this article.

CommandPrefix: This is a child element of VoiceCommands which can be optional, but if present, must be the first child element of CommandSet. This element is used for giving name for application which user can speak in order to give voice command hence it is useful for the applications which have long and difficult names to pronounce. In above example, command prefix is “hello” that means if user wants to give command to this application then he must use “hello” word at the start of any command.

Command: This is compulsory child element for CommandSet Element. This element defines action which needs to be taken after receiving voice command. Each command corresponds to specific page in your application, you can have multiple commands.

To get "did you know?" screen of your application.

  1. Make sure that VCD file of your application is successfully installed, Hold start key until "Listening" popup appears.
  2. Now click question mark icon on "Listening" popup it will open "WHAT CAN I SAY?" pivot item.
  3. Now go to the "apps" section where you can see list of applications which supports voice commands, clicking on application you want leads to "did you know?" screen. Refer the following screen shots.
WHAT CAN I SAY? did you know?
Whatcanisayapp.png Diduknow.png

Listenfor: Compulsory child element of Command element, which defines text that Application recognizes for command. As shown in above VCD file you can use curly and square braces these are special characters. Curly braces represents Phrase list and square braces represent optional text means user does not have any compulsion for speaking text defined in square bracket. Single ListenFor element may refer to one or more PhraseList as shown in above example of vcd file Calculator command refers to three PhraseList namely operand1, operation, operand2 whereas Util command refers single PhraseList namely options. Note you cant nest the special characters.


FeedBack UI




Installing/Registering voice command definition file

Installing VCD file registers voice commands defined in the VCD file to the speech system of phone, You should install the VCD file during the first run of your application. In case your application re installs due to Phone Backup no data related to your voice commands is preserved so it is better practice to install VCD file during each time your application launches.
You can use InstalledCommandSets property of VoiceCommandService class to check existence of your voice commands.

private async void RegisterVoiceCommands() 


You can use OnNavaigatedTo() method to extract parameter from QueryString using NavigationContext.



Handling navigation









voiceCommandName parameter contains command name.
reco parameter contains whole text which is expected command .
options, operand2, operation, operand1 contain recognized words from phrase list.

Refer following code snippet to know how to retrieve parameters from query string for above mentioned vcd file.

protected override void OnNavigatedTo(NavigationEventArgs e)
if (NavigationContext.QueryString.ContainsKey("voiceCommandName"))
string voicecommandname = NavigationContext.QueryString["voiceCommandName"];
switch (voicecommandname)
case "Util":
string options = NavigationContext.QueryString["options"];
//do something
case "Calculator":
string number1 = NavigationContext.QueryString["operand1"];
string number2 = NavigationContext.QueryString["operand2"];
string oprator = NavigationContext.QueryString["operation"];
//do something
catch (Exception ex)

Note switch case is used because application may have number of commands which targets same page, so it become useful to know which command user has given. In each case i mentioned do something but you can refer attached example which covers proper implementation of each voice command like showing date adding two numbers etc.

Modify Voice Command Definition file

In most of the cases we want PhraseList's content to be populated at run time using webservice or some other source, To achieve this functionality SDK gives you ability to modify voice command definition file Below is code snippets showing how to modify VCD file.


private void Button_Click_1(object sender, RoutedEventArgs e)
ModifyPhraseList("MyVoiceCommands", "operand1");
ModifyPhraseList("MyVoiceCommands", "operand2");
private async void ModifyPhraseList(string commandsetname,string phraselistname)
var vcs = VoiceCommandService.InstalledCommandSets[commandsetname];
await vcs.UpdatePhraseListAsync(phraselistname, numbers);

Sample Voice Commands


  1. hello what is current date
  2. hello what is current battery percentage.
  3. hello two plus two.
  4. hello two minus two.
Command KeyWord Specification
hello what is current date hello CommandPrefix
current date PhraseList “options”
hello two plus two hello CommandPrefix
two PhraseList “operand1”
plus PhraseList “operation”
two PhraseList “operand2”

Localization of voice commands

You can specify commands in voice command definition file for various languages.
You can create multiple CommandSets and set xml:lang to the language you want, to test application for particular language make sure that CommandSet language must match with language selected on phone.


Find complete working example here File:WinPhoneVoiceCommands.zip

437 page views in the last 30 days.