微软小娜开发教程

利用语音指令通过Cortana开启前台应用


利用语音指令除了能打开系统应用之外,还能打开前台应用并在应用内指定执行的操作或命令。

1、定义语音指令文件

新建一个空的UWP应用,往工程中添加一个空的XML文件。
语音指令特指在VCD(voice command definition)文件中按照一定格式定义的词语组合。
举个栗子,这个VCD文件是一个名叫“Adventure Works”的工程下的一个XML文件:

<?xml version="1.0" encoding="utf-8"?>
<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.1">
  <CommandSet xml:lang="en-us" Name="AdventureWorksCommandSet_en-us">
    <CommandPrefix> Adventure Works, </CommandPrefix>
    <Example> 显示伦敦之行 </Example>

    <Command Name="showTripToDestination">
      <Example> 显示伦敦之行  </Example>
      <ListenFor> 显示去 {destination} 的旅行</ListenFor>
      <Feedback> 正在显示去 {destination} 的旅行</Feedback>
      <Navigate/>
    </Command>

    <PhraseList Label="destination">
      <Item> 伦敦</Item>
      <Item> 纽约</Item>
      <Item> 北京</Item>
    </PhraseList>

    <PhraseTopic Label="newDestination" Scenario="Search">
      <Subject>City/State</Subject>
    </PhraseTopic>
  </CommandSet>

<!-- Other CommandSets for other languages -->
</VoiceCommands>

这个XML文件里就定义了一个语音命令”Adventure Works,显示去伦敦的旅行“。这里

的语音指令很灵活,可以在PhraseList 标签中动态添加。
VCD文件动态添加指令指南

2、安装VCD命令

当应用激活时,需要在OnLaunched处理程序中调用InstallCommandDefinitionsFromStorageFileAsync以注册系统应该侦听的命令。
在App.xaml.cs文件下的Onlaunched函数中添加如下代码段

.......
Window.Current.Activate();
            try
            {
                var storageFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(
                    new Uri("ms-appx:///VoiceCommands.xml"));
                await
                    Windows.ApplicationModel.VoiceCommands.VoiceCommandDefinitionManager.
                    InstallCommandDefinitionsFromStorageFileAsync(storageFile);
            }
            catch (Exception)
            {

                throw;
            }

处理激活并执行语音命令

安装语音指令集后,指定应用如何响应后续指定操作或指令动作。例如,你的应用可能导航到特定页的内容、显示地图或说出确认语句等。
执行语音指令的操作,是最终最终愿景。需要在重写OnActivated()函数里干三件事:
1. 确认你的应用已经通过语音命令激活。
2. 确认命令名称和说出的内容。
3. 执行动作,通常是导航到相关页面。

这里写代码片
protected override void OnActivated(IActivatedEventArgs e)
{
  // Was the app activated by a voice command?
  if (e.Kind != Windows.ApplicationModel.Activation.ActivationKind.VoiceCommand)
  {
    return;
  }

  var commandArgs = e as Windows.ApplicationModel.Activation.VoiceCommandActivatedEventArgs;

  Windows.ApplicationModel.VoiceCommands.VoiceCommand.SpeechRecognitionResult speechRecognitionResult = 
    commandArgs.Result;

  // Get the name of the voice command and the text spoken
  string voiceCommandName = speechRecognitionResult.RulePath[0];
  string textSpoken = speechRecognitionResult.Text;
  // The commandMode is either "voice" or "text", and it indicates how the voice command was entered by the user.
  // Apps should respect "text" mode by providing feedback in a silent form.
  string commandMode = this.SemanticInterpretation("commandMode", speechRecognitionResult);

  switch (voiceCommandName)
  {
    case "showTripToDestination":
    // Access the value of the {destination} phrase in the voice command
    string destination = speechRecognitionResult.SemanticInterpretation.Properties["destination"][0];
    // Create a navigation parameter string to pass to the page
    navigationParameterString = string.Format("{0}|{1}|{2}|{3}", 
                    voiceCommandName, commandMode, textSpoken, destination);
    // Set the page where to navigate for this voice command
    navigateToPageType = typeof(TripPage);
    break;

    default:
      // There is no match for the voice command name. Navigate to MainPage
      navigateToPageType = typeof(MainPage);
      break;
  }
  if (this.rootFrame == null)
  {
    // App needs to create a new Frame, not shown
  }

  if (!this.rootFrame.Navigate(navigateToPageType, navigationParameterString))
    {
    throw new Exception("Failed to create voice command page");
    }
}

到此为止,一个简单的通过小娜来开启前台的应用就完成了。
下面提供两个实用例程,源文件可下载。
1、通过小娜来开启一个简单的前台应用
2、Cortana语音命令实例–MSDN_demo

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章