Launching your Windows Phone App using Voice Commands

The Speech API in Windows Phone 8 allows the developers to integrate the Voice Command functionality in their Windows Phone which in turn allows the users to

  • Launch the Windows Phone App using Voice Commands.
  • Perform Actions within the App.

Launching your Windows Phone App using Voice Commands A simple example of using voice command in windows phone is to press and hold the start button for few seconds which displays the speech recognizer where the user can speak the commands that are already subscribed the application . Example 1. Call Senthil Kumar – This will pick up the contact number from the people’s hub and make a call to senthil kumar. 2. Open Calculator – This command will open the calculator application in the windows phone.

Launching your Windows Phone App using Voice Commands

Launching your Windows Phone App using Voice Commands

To integrated the Voice Command feature in your Windows Phone App, one needs to follow the below steps.

  • Create the Voice Command Definition file.
  • Register the Voice Command file in your app.
  • Add code in your page to handle the navigation and perform actions accordingly.

1. Voice Command Definition (VCD) file

The Voice Command Definition (VCD) file is a XML document which includes the definition of all the commands that are used in the app which can be used to perform action when your app is launched via voice commands. Below is a sample VCD file which will be used as example. Let’s name the file a VoiceCommandDefinition1.xml

<?xml version="1.0" encoding="utf-8"?>
<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.0">
 <CommandSet xml:lang="en-US">
  <CommandPrefix>MobileOSGeek</CommandPrefix>
  <Example> Show me the events </Example>
  <Command Name="EventsFilter">
    <Example> Show me the List of Events </Example>
    <ListenFor> [Show me events in ] {MonthViews} </ListenFor>
    <Feedback> Showing the events in {MonthViews} </Feedback>
    <Navigate />
  </Command>
  <PhraseList Label="MonthViews">
    <Item> May </Item>
    <Item> June </Item>
  </PhraseList>
 </CommandSet>
</VoiceCommands>

The Voice Command Definition file contains the root element VoiceCommands which has one or more CommandSet elements. In the above example, the voice commands consists of CommandSet supporting the language en-US. The CommandSet consists of the elements CommandPrefix, Example, Command, and Phrase List. The CommandPrefix is like an alias name which the user uses to launch the app via voice command by tapping and holding the start button and speaking the CommandPrefix + command in the Speech Recognizer interface. The Example element identifies the text that will be used on the Speech Recognition dialog. The CommandSet has the Command element which needs atleast one of more ListenFor elements which defines the speech that activates a command. The Square bracket denotes that the text is optional whereas the curly braces denotes the phrase list within the document. With the above VCD file, the user can launch the app with the following commands. 1. MobileOSGeek Show me events in June 2. MobileOSGeek Show me events in May 3. MobileOSGeek June 4. MobileOSGeek May The Feedback element defines the response to the user after the command is recognised. The Navigate element is used to specify the page to navigate to when the command is identified. By default, it takes the start page of your application.

2. Integrate or Register the Voice Command Definition file.

The next step to using the voice command is to register the VCD file programatically within your app. Once the VCD file is registered, the user will be able to launch your windows phone app by tapping and holding the windows start button and speaking the registered command. Below is an example on how to register the VCD file.

// Code to execute when the application is launching (eg, from Start)
// This code will not execute when the application is reactivated
async private void Application_Launching(object sender, LaunchingEventArgs e)
{
    await Windows.Phone.Speech.VoiceCommands.VoiceCommandService.InstallCommandSetsFromFileAsync(
    new Uri("ms-appx:///VoiceCommandDefinition1.xml"));
}

3. Handle Navigation and Perform Action

The Last step to implement voice command in your app is to handle the navigation via the OnNavigatedTo event of the page and perform action based on the command spoken. When the user the command, the information is passed to your app as a query string to the page specified in the Navigate element of the comment. For example, when the command spoken by the user is “MobileOSGeek Show me events in June”, the following query string is generated and passed to the MainPage.xaml

“/MainPage.xaml?voiceCommandName=EventsFilter&reco=MobileOSGeek%20Show%20me%20events%20in%20June&MonthViews=June”

Launching your Windows Phone App using Voice Commands

Below is an example of the OnNavigatedTo event demonstrating the actions performed based on the voice command.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
  base.OnNavigatedTo(e);
  string Url = e.Uri.ToString();
  if (NavigationContext.QueryString.ContainsKey("voiceCommandName"))
  {
    string CommandName = NavigationContext.QueryString["voiceCommandName"];
    switch (CommandName)
    {
        case "EventsFilter":
          string viewWidget = NavigationContext.QueryString["MonthViews"];
          var data = UGMeets.Where(a => a.Month == viewWidget).ToList();
          LLS.ItemsSource = data;
        break;
    }
  }
}
Launching your Windows Phone App using Voice Commands

Launching your Windows Phone App using Voice Commands

Launching your Windows Phone App using Voice Commands

Launching your Windows Phone App using Voice Commands

Note: To use the speech feature in your windows phone app, don’t forget to enable the following capabilities in your app manifest file. <Capability Name=”ID_CAP_MICROPHONE” /> <Capability Name=”ID_CAP_SPEECH_RECOGNITION” /> Below is a complete code snippet of the MainPage.xaml.cs file

using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using VoiceCommandDemo1.Resources;
namespace VoiceCommandDemo1
{
  public partial class MainPage : PhoneApplicationPage
  {
  // Constructor
    public MainPage()
    {
      InitializeComponent();
      UGMeets.Add(new Events { Month = "May", Name = "BDotnet Windows Phone Webinars" });
      UGMeets.Add(new Events { Month = "October", Name = "Windows Phone Week" });
      UGMeets.Add(new Events { Month = "June", Name = "Powershell UG Meet" });
      UGMeets.Add(new Events { Month = "June", Name = "SQL Bangalore Meet" });
    }
    private List<Events> UGMeets = new List<Events>();
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
      base.OnNavigatedTo(e);
      string Url = e.Uri.ToString();
      if (NavigationContext.QueryString.ContainsKey("voiceCommandName"))
      {
        string CommandName = NavigationContext.QueryString["voiceCommandName"];
        switch (CommandName)
        {
            case "EventsFilter":
            string viewWidget = NavigationContext.QueryString["MonthViews"];
            var data = UGMeets.Where(a => a.Month == viewWidget).ToList();
            LLS.ItemsSource = data;
            break;
        }
      }
    }
  }
  public class Events
  {
     public string Month { get; set; }
     public string Name { get; set; }
  }

}

You can download the code sample used in this article here.

Leave a Reply