在没有Windows依赖性的Web应用程序中集成Cortana语音命令

时间:2016-03-12 04:17:39

标签: web voice-recognition cortana

是否可以在基于javascript的通用Web应用程序中使用Cortana语音命令功能,该应用程序能够在任何webbrowser /平台上运行? (例如通过html5语音输入和某种类型的网络服务)或者网页是否必须在安装了Cortana客户端的基于Windows的计算机上加载?

我遇到了这个示例,但似乎期望在运行该页面的设备上安装Cortana: https://gist.github.com/seksenov/17032e9a6eb9c17f88b5

1 个答案:

答案 0 :(得分:0)

这是一个基本示例,您可以编辑为您的应用程序运行的代码: https://gist.github.com/seksenov/17032e9a6eb9c17f88b5

cortana.html

<!DOCTYPE html>
<html>
<head>
    <title>Cortana Example</title>
    <!--Cortana meta tag pointing to VCD file on the server-->
    <meta name="msapplication-cortanavcd" content="/vcd.xml"/>
</head>
<body>

</body>
</html>

cortana.js

if (typeof Windows !== 'undefined' &&
    typeof Windows.UI !== 'undefined' &&
    typeof Windows.ApplicationModel !== 'undefined') 
  {
  // Subscribe to the Windows Activation Event
  Windows.UI.WebUI.WebUIApplication.addEventListener("activated", function (args) {
    var activation = Windows.ApplicationModel.Activation;
    // Check to see if the app was activated by a voice command
    if (args.kind === activation.ActivationKind.voiceCommand) {
      // Get the speech reco
      var speechRecognitionResult = args.result;
      var textSpoken = speechRecognitionResult.text;
      // Determine the command type {search} defined in vcd
      if (speechRecognitionResult.rulePath[0] === "search") {
        // Determine the stream name specified
        if (textSpoken.includes('foo') || textSpoken.includes('Foo')) {
          console.log("The user is searching for foo");
        }
        else if (textSpoken.includes('bar') || textSpoken.includes('Bar') ) {
          console.log("The user is searching for a bar");
        }
        else {
          console.log("Invalid search term specified by user");
        }
      }
      else { 
        console.log("No valid command specified");
      }
    }
  });
} else {
  console.log("Windows namespace is unavaiable");
}

vcd.xml

<?xml version="1.0" encoding="utf-8"?>
<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.2">
  <CommandSet xml:lang="en-us" Name="RJS">
    <CommandPrefix>MyApp</CommandPrefix>
    <Example>MyApp search for foo</Example>
    <Command Name="Search">
      <Example>search {message} using myapp</Example>
      <ListenFor RequireAppName="BeforeOrAfterPhrase">Search for {searchTerm}</ListenFor>
      <Feedback>Searching for {searchTerm} with MyApp</Feedback>
      <Navigate Target="/search.htm"/>
    </Command>
    <PhraseTopic Label="searchTerm" Scenario="Dictation"></PhraseTopic>
  </CommandSet>
</VoiceCommands>