那么如何讓用戶通過小娜來和你的應(yīng)用來進(jìn)行交互呢?下面我們來通過一個(gè)例子來學(xué)習(xí)如何使用小娜。
首先是VS一定要是Visual Studio 2013 Update 2 或者 更新的,我這里使用的Update3
然后需要安裝 Windows Phone 8.1 Update and Emulators.
Windows Phone 8.1 Update and Emulators下載地址
如何開啟模擬的小娜請看我之前的博文:Cortana(小娜)那些事兒
ps. 建議安裝vs的時(shí)候使用英文版,因?yàn)楸救嗽谥鞍惭b的中文版中出現(xiàn)了在創(chuàng)建vcd文件時(shí)找不到http://schemas.microsoft.com/voicecommands/1.1 的情況,如果有類似情況的同學(xué),可是嘗試重新安裝VS的英文版。然后再安裝Windows Phone 8.1 Update and Emulators 問題應(yīng)該就解決了. 建議使用真機(jī)進(jìn)行調(diào)試。因?yàn)楸救嗽谑褂媚M器調(diào)試時(shí),無論使用任何命令都會(huì)跳轉(zhuǎn)到bing搜索。所以本人改用真機(jī)。
這里我打算開發(fā)一個(gè)圖片搜索應(yīng)用來作為一個(gè)例子。 功能很簡單將用戶的語音命令直接到bing去搜索相應(yīng)的圖片。然后返回給用戶。 (為了更專注于Cortana的功能- -就沒寫搜索功能,僅僅是簡單的將用戶的語音識別結(jié)果傳送出來)
VCD(Voice Command Definition)文件是一個(gè)xml文檔,用來定義所有你可以使用的語音命令。具體的使用請參考如下兩個(gè)連接
Voice Command Definition (VCD) elements and attributes
首先在項(xiàng)目上右擊選擇添加新的項(xiàng)目如下圖所示。
選擇vcd文件
創(chuàng)建好文件后打開文件發(fā)現(xiàn)里面如下圖已經(jīng)有了很多代碼,好我們先全部都刪掉。
替換為我們的代碼
<?xml version="1.0" encoding="utf-8"?><VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.1"> <CommandSet xml:lang="zh-cn" Name="englishCommands"> <CommandPrefix> Image </CommandPrefix> <Example> Image </Example> <Command Name="ImageSearch"> <Example> find 'something' </Example> <ListenFor> Search </ListenFor> <ListenFor> Search [for] {dictatedSearchTerms} </ListenFor> <ListenFor> Find {dictatedSearchTerms} </ListenFor> <ListenFor> Find </ListenFor> <Feedback> Searching on Bing...</Feedback> <Navigate Target="MainPage.xaml" /> </Command> <PhraseTopic Label="dictatedSearchTerms" Scenario="Search"> <Subject> ImageName </Subject> </PhraseTopic> </CommandSet></VoiceCommands>
創(chuàng)建好VCD文件是不夠的,你必須安裝你的VCD文件到你的系統(tǒng)上。如何安裝呢?其實(shí)很簡單就是在啟動(dòng)你的應(yīng)用時(shí)能夠執(zhí)行一句安裝vcd文件的命令即可。這里我們在Mainpage的OnNavigatedTo方法中來注冊。復(fù)制下面的代碼即可(別忘記把xml文件的地址改成自己的哦)
protected override async void OnNavigatedTo(NavigationEventArgs e) { if (e.NavigationMode == NavigationMode.New) { var storageFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///VoiceCommandDefinition.xml")); await Windows.Media.SpeechRecognition.VoiceCommandManager.InstallCommandSetsFromStorageFileAsync(storageFile); } }
如果上面的都做完的你就可以嘗試的運(yùn)行下程序了,為了確定你vcd是否安裝成功你可以打開小娜的查看更多選項(xiàng)然后拉倒最下面??纯茨愕膽?yīng)用是不是在下面(如下圖所示),如果有了說明你的vcd文件已經(jīng)注冊成功,如果沒有請確認(rèn)的你vcd文件中的語言是否和你的設(shè)備的語言環(huán)境是相同的,如果不同也可能會(huì)無法安裝你的vcd文件。
ps. 注意你一定要在package.appxmanifest中開啟Microphone不然運(yùn)行會(huì)出現(xiàn)問題(語音功能不開mic怎么能行呢是吧^_^)
做完上面所有的事情后就剩下處理我們的語音命令了。
首先我們需要重寫Application.OnActivated事件。確定是不是語音命令來啟動(dòng)我們的應(yīng)用 的(通過判斷IActivatedEventArgs.Kind 是不是 VoiceCommand)
然后我們獲取我們語音命令的結(jié)果。
我嘗試改寫MSDN的例子并應(yīng)用到我們的例子上:
protected override void OnActivated(IActivatedEventArgs args) { Frame rootFrame = Window.Current.Content as Frame; // 判斷是否由語音命令啟動(dòng) if (args.Kind == Windows.ApplicationModel.Activation.ActivationKind.VoiceCommand) { var commandArgs = args as Windows.ApplicationModel.Activation.VoiceCommandActivatedEventArgs; Windows.Media.SpeechRecognition.SpeechRecognitionResult speechRecognitionResult = commandArgs.Result; // 如果是的話獲取語音命令的名字(vcd文件中的Command Name), 命令后用戶說出的信息 以及 vcd Command/Navigate@Target 的值. string voiceCommandName = speechRecognitionResult.RulePath[0]; string textSpoken = speechRecognitionResult.Text; string navigationTarget = speechRecognitionResult.SemanticInterpretation.Properties["NavigationTarget"][0]; //判斷當(dāng)前命令式什么 <del>T_T 這里只有一個(gè)</del><del> switch (voiceCommandName) { case "ImageSearch": //跳轉(zhuǎn)到相應(yīng)的page,并將所有說的東西都傳過去。(為什么這么跳轉(zhuǎn)?因?yàn)閷懙目? -|||) rootFrame.Navigate(typeof(ShowImage), textSpoken); break; default: rootFrame.Navigate(typeof(ShowImage), "nothing"); break; } } }
然后創(chuàng)建一個(gè)新的page c#
<Page x:Class="ImageFinder.ShowImage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:ImageFinder" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> <TextBlock x:Name="textBlock" FontSize="50" Text="nothing"></TextBlock> </Grid></Page>
Xaml
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation;// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556namespace ImageFinder { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class ShowImage : Page { public ShowImage() { this.InitializeComponent(); } /// <summary> /// Invoked when this page is about to be displayed in a Frame. /// </summary> /// <param name="e">Event data that describes how this page was reached. /// This parameter is typically used to configure the page.</param> protected override void OnNavigatedTo(NavigationEventArgs e) { textBlock.Text = e.Parameter.ToString(); } }}
打開小娜對著她說 ImageFinder Search 測試 (呵呵中英混合看看她識別的怎么樣) 非常nice她打開了我的應(yīng)用并跳轉(zhuǎn)到相應(yīng)的頁面,也將我的話打印出來了。
ps注意測試時(shí)盡量使用語音,使用文字可能會(huì)出現(xiàn)直接跳轉(zhuǎn)到bing的情況,也許是因?yàn)樾∧痊F(xiàn)在還是bete版本的情況吧。
好了文章就到這里了,如果你在開發(fā)中遇到什么問題(我就遇到了不少= =)調(diào)試過不去請聯(lián)系我的微博http://www.weibo.com/songzy982 我會(huì)盡力幫你解答的。