Iris Classon
Iris Classon - In Love with Code

Advanced WP8 in easy steps: Speech

Bits and pieces from my slides and code from my session on WP8 at Norwegian Developer Conference 2013

Speech is a rather neat feature of Windows Phone, and we can now do more than just launch an application. Integrating speech in the app is easy, and allows the user one more way to interact with the application. To use voice commands hold down the windows ‘button’ until you are prompted to make request a command.

Three main concepts in speech:


Note that speech is also accessible from the lock screen – but this has to be enabled in settings (settings- speech).

Voice commands are for sending commands to an app with pre-specified arguments (the so-called phrase list).

Speech recognition is used from within the app.

Text to speech will read out specified text in languages supported and chosen. Default language is locale.

The steps


Let’s walk through the steps:

1. Capability

2. Namespace

 using Windows.Phone.Speech.Synthesis; 
  using Windows.Phone.Speech.VoiceCommands; 
  using Windows.Phone.Speech.Recognition; 

3. Add VCD file

Right click on project, Add new file. Select VCD – Voice Command Definition file. The file will have an example in it.

[sourcecode language=“XML”]


CommandSetallows you to set language

CommandPrefix is what you want the user to call the app,

Command is what the user wants the app to do.

Example is an example provided to the user so they know how they can call the command

ListenFor is what we expect the user to say. Square brackets are optional words, and brackets are arguments passed to the command.

The arguments the user can use is added in a PhraseList. this list can also be updated dynamically.

4. Register Service

Do this when the app starts up, the app has to to run at least once before commands can be used.

Example code:

await VoiceCommandService.InstallCommandSetsFromFileAsync(new Uri(“ms-appx:///ex.xml”));

5. Handle invocation

Once a command is called you need to handle invocation. Here is an example, notice that we use querystring property on the navigationcontext to get our arguments and command name.

[sourcecode language=“csharp”]
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
// New or tombstone?
if (e.NavigationMode != System.Windows.Navigation.NavigationMode.New) return;
// Voice command?
if (!NavigationContext.QueryString.ContainsKey(“voiceCommandName”)) return;

        var voiceCommandName = NavigationContext.QueryString["voiceCommandName"];  

        if (voiceCommandName != "AddItem") return;  
        var nr = NavigationContext.QueryString["number"];  
        var type = NavigationContext.QueryString["type"];  

        NavigationService.Navigate(new Uri("/SpeechPage.xaml?number=" + nr + "&type=" + type, UriKind.RelativeOrAbsolute));  


Providing feedback by using text to speech:

 <strong>await new SpeechSynthesizer().SpeakTextAsync(&quot;Say this&quot;);</strong> 


Leave a comment below, or by email.

Last modified on 2013-06-15

comments powered by Disqus