Skip to content

SpeechNavigator integration tutorial

Jakub Lichman edited this page Apr 26, 2017 · 4 revisions

In this tutorial we are going to show you how to integrate your UWP app with voice control. If you will have some problems with code below, please refer to FAQ page for possible solutions.

Step1 - Create VCD file

  1. If you still have some problems with creation of VCD file, please check this article.
  2. If you don't have enough examples, please refer to commands that are used in our SpeechNavigator.

Step2 - Install VCD file into SpeechNavigator

Second step in integration is to create SpeechNavigator with specific language and commands.
Note: Just CommandSet thats value of xml:lang attribute matches the provided language is installed.

//get file with VCD commands
var navigatorCommandsPath = @"Path\to\your\commands.xml";
var navigatorCommandsFile = await StorageFile.GetFileFromPathAsync(navigatorCommandsPath);
//Create new instance of SpeechNavigator
var navigator = await SpeechNavigator.Create(navigatorCommandsFile, new Windows.Globalization.Language("en-us"));
//Set listening timeouts. Default value is 5 seconds.
navigator.Timeouts.InitialSilenceTimeout = new TimeSpan(0, 0, 10);

Step3

After successful installation we need to attach actions to commands. We will do it by calling SetAction method. First argument specifies command set, where is command located, second one our targeted command and third one callback to the action that will be performed after successful recognition.
Here is full example including previous step:

private SpeechNavigator m_navigator;
private async void RecognitionButton_Clicked(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
    if(m_navigator == null)
        return;
    try
    {
        await m_navigator.RecognizeAndPerformActionAsync();
    }
    catch (Exception ex)
    {
        if ((uint)ex.HResult == SpeechNavigator.HResultPrivacyStatementDeclined)
        {
            //notice user to accept privacy settings
            LaunchUri(new Uri(@"https://privacy.microsoft.com/en-us/privacystatement"));
            //Todo display user Privacy settings 
        }
        else if (ex.GetType() == typeof(UnauthorizedAccessException))
        {
            LaunchUri(new Uri("ms-settings:privacy-microphone"));
        }
        else
        {
	     //catch another exception
        }
    }
}

private async void callback1(object sender, SpeechRecognitionResult e)
{
    //your code here
}

private async void callback2(object sender, SpeechRecognitionResult e)
{
    //your code here
}

//initialize SpeechNavigator during launch
protected override async void OnLaunched(LaunchActivatedEventArgs e)
{
    //get file with VCD commands
    var navigatorCommandsPath = @"Path\to\your\commands.xml";
    var navigatorCommandsFile = await StorageFile.GetFileFromPathAsync(navigatorCommandsPath);
    //Create new instance of SpeechNavigator
    m_navigator = await SpeechNavigator.Create(navigatorCommandsFile, new Windows.Globalization.Language("en-us"));
    //Set listening timeouts. Default value is 5 seconds.
    m_navigator.Timeouts.InitialSilenceTimeout = new TimeSpan(0, 0, 10);

    //now we will set actions to navigator
    m_navigator.SetAction("MyCommandSet", "MyCommand1", callback1);
    m_navigator.SetAction("MyCommandSet", "MyCommand2", callback2);
}
Clone this wiki locally