Cortana没有拿起命令参数

时间:2016-01-29 22:20:15

标签: windows-10 win-universal-app cortana hosted-app

我正在使用Windows 10 UWP托管Web应用程序,我尝试使用vcd文件添加Cortana支持。我有vcd文件,元标记和js文件来处理语音命令,但是当我构建并运行应用程序时,Cortana没有获取命令参数。

示例vcd.xml文件

<?xml version="1.0" encoding="utf-8"?>
<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.2">
    <CommandSet xml:lang="en-us" Name="VoiceDemoCommandSet_en-us">
        <AppName>VoiceDemo</AppName>
        <Example>VoiceDemo search for foo</Example>
        <Command Name="Search">
            <Example>search {message} using VoiceDemo</Example>
            <ListenFor RequireAppName="BeforeOrAfterPhrase">Search {searchTerm}</ListenFor>
            <Feedback>Searching for "{searchTerm}" with VoiceDemo</Feedback>
            <Navigate Target="/home/about"/>
        </Command>
        <PhraseTopic Label="searchTerm" Scenario="Natural Language"/>
    </CommandSet>
</VoiceCommands>

当我对Cortana&#34; VoiceDemo搜索foo&#34;时说。

带回Cortana

搜索&#34; ...&#34;使用VoiceDemo

在javascript代码中,我获取了传入的voiceCommand对象,但结果属性设置为&#34;搜索...&#34;。我是否遗漏了vcd.xml文件?

Javascript代码

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) {

            var speechRecognitionResult = args.result;
            var textSpoken = speechRecognitionResult.text;

            // Determine the command type {search} defined in vcd
            if (speechRecognitionResult.rulePath[0] === "Search") {
                console.log("speechRecognitionResult: " + speechRecognitionResult);
                console.log("textSpoken: " + textSpoken);

                // Build rest of search string here
                // Then invoke search
            }
            else {
                console.log("No valid command specified");
            }
        }
    });
} else {
    console.log("Windows namespace is unavaiable");
}

Cortana展示的内容:

Cortana screen shot

2 个答案:

答案 0 :(得分:1)

我今天早上遇到了这个问题。对我来说,这只是缺乏互联网连接。没有互联网,我就得到了......#34;。确保互联网连接后,我得到了正确的搜索短语。

答案 1 :(得分:0)

根据我们评论中的信息,最可能出现的问题是vcd文件中的新命令代码。新命令可能与旧命令冲突,因此它会以旧方式处理这个新命令。

我添加了这样的新语音命令:

<?xml version="1.0" encoding="utf-8" ?>
<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.2">
  <CommandSet xml:lang="en-us" Name="VoiceDemoCommandSet_en-us">
    <AppName>VoiceDemo</AppName>
    <Example>VoiceDemo search for foo</Example>
    <Command Name="Search">
      <Example>search {message} using VoiceDemo</Example>
      <ListenFor RequireAppName="BeforeOrAfterPhrase">Search {searchTerm}</ListenFor>
      <Feedback>Searching "{searchTerm}" with VoiceDemo</Feedback>
      <Navigate Target="/home/about" />
    </Command>
    <Command Name="Open">
      <Example>open {message} using VoiceDemo</Example>
      <ListenFor RequireAppName="BeforeOrAfterPhrase">Open {openTerm}</ListenFor>
      <Feedback>Opening "{openTerm}" with VoiceDemo</Feedback>
      <Navigate Target="/home/about" />
    </Command>
    <Command Name="Find">
      <Example>find {message} using VoiceDemo</Example>
      <ListenFor RequireAppName="BeforeOrAfterPhrase">Find {openTerm}</ListenFor>
      <Feedback>Finding "{openTerm}" with VoiceDemo</Feedback>
      <Navigate Target="/home/about" />
    </Command>
    <PhraseTopic Label="searchTerm" Scenario="Natural Language" />
    <PhraseTopic Label="openTerm" />
  </CommandSet>
</VoiceCommands>

我在web应用程序的js文件中处理新的Open命令,如下所示:

if (args.kind === activation.ActivationKind.voiceCommand) {
    var speechRecognitionResult = args.result;
    var textSpoken = speechRecognitionResult.text;

    // Determine the command type {search} defined in vcd
    if (speechRecognitionResult.rulePath[0] === "Search") {
        console.log("speechRecognitionResult: " + speechRecognitionResult);
        console.log("textSpoken: " + textSpoken);
        document.getElementById("txt").innerText = "search";
        // Build rest of search string here
        // Then invoke search
    }
    else if (speechRecognitionResult.rulePath[0] === "Open") {
        console.log("speechRecognitionResult: " + speechRecognitionResult);
        console.log("textSpoken: " + textSpoken);
        document.getElementById("txt").innerText = "open";
    }
    else {
        console.log("No valid command specified");
        document.getElementById("txt").innerText = "else";
    }
}

我更新了我的sample on GitHub,因此您可以测试它,我没有专门处理新命令&#34;查找&#34;,当您要求Cortana进行语音演示查找时abc&#34;,它将打开VoiceDemo应用程序,并显示&#34; else&#34;在其内容中。