1

我正在开发 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 说“VoiceDemo 搜索 foo”时。Cortana 回来了

使用 VoiceDemo 搜索“...”

在 javascript 代码中,我获取了传入的 voiceCommand 对象,但结果属性设置为“搜索 ...”。我是否缺少 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 屏幕截图

4

2 回答 2

1

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

于 2016-06-13T18:09:35.267 回答
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>

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

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";
    }
}

我在 GitHub 上更新了我的示例,所以你可以测试一下,我没有专门处理新命令“Find”,当你要求 Cortana 进行“语音演示 find abc”时,它会打开 VoiceDemo 应用程序,并显示“else”在其内容中。

于 2016-02-04T04:40:00.330 回答