Sonntag, 16. Dezember 2012

Windows Phone 8: Voice command support

I really love it, if the implementation of rich user functions is easy – and the addition of voice command support for a Windows Phone 8 app is very easy.

With Windows Phone 8 you can add additional startup commands for your app which can be used to deep link in your app functions (see details here: http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj206959(v=vs.105).aspx)

In my WorkTime app I want to have a new voice command “WorkTime add entry”.

What are the necessary steps?

Adding a new Voice Command Definition file to the project. I used two CommandSets for each supported language.

<?xml version="1.0" encoding="utf-8"?>
 
<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.0">
  <CommandSet xml:lang="de">
    <CommandPrefix>WorkTime</CommandPrefix>
    <Example> neuer Eintrag </Example>
 
    <Command Name="NewEntry">
      <Example> neuer Eintrag </Example>
      <ListenFor> neu </ListenFor>
      <ListenFor> [ein] neuer Eintrag </ListenFor>
      <ListenFor> [und] [ein] neuer Eintrag </ListenFor>
      <Feedback> WorkTime erzeugt einen neuen Eintrag... </Feedback>
      <Navigate Target="/MainPage.xaml" />
    </Command>
 
  </CommandSet>
  <CommandSet xml:lang="en-US">
    <CommandPrefix>WorkTime</CommandPrefix>
    <Example> new entry </Example>
 
    <Command Name="NewEntry">
      <Example> new entry </Example>
      <ListenFor> new </ListenFor>
      <ListenFor> [a] new entry </ListenFor>
      <ListenFor> [and] [a] new entry </ListenFor>
      <Feedback> WorkTime creating a new entry... </Feedback>
      <Navigate Target="/MainPage.xaml" />
    </Command>
 
  </CommandSet>
</VoiceCommands>

After that you have to enable the following capabilities in the AppManifest.xml

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

In the constructor of the App or your start page it is necessary to tell Windows Phone that your app supports new voice commands, which are defined in your added Voice Command Definition file (VoiceCommandDefinitionWorkTime.xml in my case)

public App()
   {
       // Global handler for uncaught exceptions.
       UnhandledException += Application_UnhandledException;
 
       // Standard XAML initialization
       InitializeComponent();
 
       // Phone-specific initialization
       InitializePhoneApplication();
 
       // Language display initialization
       InitializeLanguage();
 
       // Add voice command support
       InitalizeVoiceCommands();
 
       //Readies the DispatcherHelper for usage throughout application
       DispatcherHelper.Initialize();
private async Task InitalizeVoiceCommands()
{
    //VoiceCommandService.InstalledCommandSets.ContainsKey("en-US")
    await
        VoiceCommandService.InstallCommandSetsFromFileAsync(
            new Uri("ms-appx:///VoiceCommandDefinitionWorkTime.xml"));
}

The last step is to check on the start page or your target page (see Voice Command Definition file) if the user starts the app by this command. You can do this by overriding the OnNavigatedTo event:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
 
    if (e.NavigationMode == NavigationMode.New)
    {
        if (NavigationContext.QueryString.ContainsKey("voiceCommandName"))
        {
            var command = NavigationContext.QueryString["voiceCommandName"];
 
            if (command == "NewEntry")
            {
                var vm = this.DataContext as MainViewModel;
                if (vm != null)
                {
                    vm.AddCommand.Execute(null);
                }
            }
        }
    }
}

The result is that your app will now support a command “NewEntry” which will be executed if the user presses the Windows button for a few seconds and speaks:

“WorkTime add new entry” or “WorkTime new” or “WorkTime add a new entry”

wp_ss_20121216_0001

Keine Kommentare:

Kommentar veröffentlichen