如何將 Cortana 與 Windows Phone 8.1 應用整合 ( Voice command - Natural language recognition )

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   使用   os   strong   

隨著 Windows Phone 8.1 GDR1 + Cortana 中文版的發布,相信有很多使用者或開發人員都在調戲 Windows Phone 的語音私人助理 Cortana 吧,在世界盃的時候我親測 Cortana 預測德國和阿根廷的比賽很準的。(題外話扯遠了),可是作為開發人員我們怎麼將Cortana整合到應用中呢,今天我用一點時間給大家介紹一下如何使用 voice command 整合 Windows Phone 8.1 的應用。

首先要明確兩個名詞 Voice command & Voice Command Definition 即 VCD檔案,相信做過windows Phone 8.0 開發的朋友應該有所瞭解,通過註冊VCD檔案 Windows phone 8.0 的應用當中就可以實現 voice command 的功能,如果你不瞭解請先讀一下我之前的文章(這裡我就不在過多介紹 8.0 Voice command 的重複內容了),Windows Phone 8 語音 - Speech for Windows Phone 8 快速瞭解一下Windows Phone 開發語音功能的前期準備工作。

 

簡單的說在 Windows Phone 8.0 voice command 功能比較簡單,主要是通過 Voice Command Name 判斷預製在VCD檔案中的幾個命令。

在 Windows Phone 8.1 應用中 Cortana 提供了更強的自然語言識別(Natural language recognition)

當然 VCD 檔案的中的 grammars  也得到了擴充,並且區別兩個OS版本的

http://schemas.microsoft.com/voicecommands/1.0 for Windows Phone 8.0 Voice Command and Cortana compatible.

http://schemas.microsoft.com/voicecommands/1.1 only for Widnows Phone 8.1 Cortnan.

詳細內容請參考

Windows Phone 8.0:  Voice command element and attribute reference for Windows Phone 8Windows Phone 8.1:  Voice command elements and attributes

通過 8.0 和 8.1 VCD 檔案屬性支援情況來看有一個最主要的區別在8.1 VCD中支援 PhraseTopic 這個屬性。

文字說的太抽象了還是貼出代碼給大家說說吧:

這裡我主要強調說一下 ListenFor  結點和 PhraseTopic 結點。 注意在 Listenfor 結點中的中括弧 {dictatedSearchTerms} 是對應的 PhraseTopic  結點中的 Label 屬性。同時我們可以把 PhraseTopic 理解成任意內容。最後都可以從Cortana回傳到我們的應用當中來。

<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.1">    <!-- The CommandSet Name is used to programmatically access the CommandSet -->    <CommandSet xml:lang="zh-CN" Name="chineseCommands">        <!-- The CommandPrefix provides an alternative to your full app name for invocation -->        <CommandPrefix> 微軟 文檔 </CommandPrefix>        <!-- The CommandSet Example appears in the global help alongside your app name -->        <Example> 搜尋 構造 函數 </Example>        <Command Name="MSDNSearch">            <!-- The Command example appears in the drill-down help page for your app -->            <Example> 搜尋 構造 函數‘ </Example>            <!-- ListenFor elements provide ways to say the command, including references to             {PhraseLists} and {PhraseTopics} as well as [optional] words -->            <ListenFor> 尋找 {dictatedSearchTerms} </ListenFor>            <ListenFor> 搜 {dictatedSearchTerms} </ListenFor>            <ListenFor> 搜尋  {dictatedSearchTerms} </ListenFor>            <ListenFor> 查 {dictatedSearchTerms} </ListenFor>            <ListenFor> 找 {dictatedSearchTerms} </ListenFor>          <!--Feedback provides the displayed and spoken text when your command is triggered -->            <Feedback> 尋找 MSDN... </Feedback>            <!-- Navigate specifies the desired page or invocation destination for the Command-->            <Navigate Target="MainPage.xaml" />        </Command>        <Command Name="MSDNNaturalLanguage">            <Example> 我 想 去 Windows 手機 開發 中心 </Example>            <ListenFor> {naturalLanguage} </ListenFor>            <Feedback> 啟動 MSDN... </Feedback>            <Navigate Target="MainPage.xaml" />        </Command>        <PhraseTopic Label="dictatedSearchTerms" Scenario="Search">            <Subject> MSDN </Subject>        </PhraseTopic>        <PhraseTopic Label="naturalLanguage" Scenario="Natural Language">            <Subject> MSDN </Subject>        </PhraseTopic>    </CommandSet></VoiceCommands>

 

瞭解完新的VCD檔案,在這裡我提醒下大家,其實在Windows Phone 8.0的應用中也可以相容 Cortana的功能的,在8.0的應用當中我們只需要判斷一下作業系統的版本然後選擇不同的VCD檔案向系統註冊即可。

首先我們需要把兩個版本的VCD檔案都存放到項目中來

其次在註冊VCD檔案的時候根據系統版本進行一下判斷即可。

        /// <summary>        /// Installs the Voice Command Definition (VCD) file associated with the application.        /// Based on OS version, installs a separate document based on version 1.0 of the schema or version 1.1.        /// </summary>        private async void InstallVoiceCommands()        {            const string wp80vcdPath = "ms-appx:///VoiceCommandDefinition_8.0.xml";            const string wp81vcdPath = "ms-appx:///VoiceCommandDefinition_8.1.xml";            const string chineseWp80vcdPath = "ms-appx:///ChineseVoiceCommandDefinition_8.0.xml";            const string chineseWp81vcdPath = "ms-appx:///ChineseVoiceCommandDefinition_8.1.xml";            try            {                bool using81orAbove = ((Environment.OSVersion.Version.Major >= 8)                    && (Environment.OSVersion.Version.Minor >= 10));                string vcdPath = using81orAbove ? wp81vcdPath : wp80vcdPath;                if (InstalledSpeechRecognizers.Default.Language.Equals("zh-CN", StringComparison.InvariantCultureIgnoreCase))                {                    vcdPath = using81orAbove ? chineseWp81vcdPath : chineseWp80vcdPath;                }                Uri vcdUri = new Uri(vcdPath);                await VoiceCommandService.InstallCommandSetsFromFileAsync(vcdUri);            }            catch (Exception vcdEx)            {                Dispatcher.BeginInvoke(() =>                {                    MessageBox.Show(String.Format(                        AppResources.VoiceCommandInstallErrorTemplate, vcdEx.HResult, vcdEx.Message));                });            }        }

最後在應用當中擷取使用者的語音輸入方法,注意這裡也是需要通過 PhraseTopic 結點的 Label 名稱擷取的。

        /// <summary>        /// Takes specific action for a retrieved VoiceCommand name.        /// </summary>        /// <param name="voiceCommandName"> the command name triggered to activate the application </param>        private void HandleVoiceCommand(string voiceCommandName)        {            // Voice Commands can be typed into Cortana; when this happens, "voiceCommandMode" is populated with the            // "textInput" value. In these cases, we‘ll want to behave a little differently by not speaking back.            bool typedVoiceCommand = (NavigationContext.QueryString.ContainsKey("commandMode")                 && (NavigationContext.QueryString["commandMode"] == "text"));            string phraseTopicContents = null;            bool doSearch = false;            switch (voiceCommandName)            {                case "MSDNNaturalLanguage":                    if (NavigationContext.QueryString.TryGetValue("naturalLanguage", out phraseTopicContents)                        && !String.IsNullOrEmpty(phraseTopicContents))                    {                        // We‘ll try to process the input as a natural language query; if we‘re successful, we won‘t                        // fall back into searching, since the query will have already been handled.                        doSearch = TryHandleNlQuery(phraseTopicContents, typedVoiceCommand);                    }                    break;                case "MSDNSearch":                    // The user explicitly asked to search, so we‘ll attempt to retrieve the query.                    NavigationContext.QueryString.TryGetValue("dictatedSearchTerms", out phraseTopicContents);                    doSearch = true;                    break;            }            if (doSearch)            {                HandleSearchQuery(phraseTopicContents, typedVoiceCommand);            }        }

整個過程就這麼簡單,心動不如行動,趕快把你的應用加入Cortana 功能讓小夥伴兒們調戲一番。

更多參考資料:

Quickstart: Voice commands (XAML)

Speech for Windows Phone 8

快速入門:語音命令 (XAML)

源碼下載:

MSDN Voice Search for Windows Phone 8.1

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.