With the release of the Chinese version of Windows Phone 8.1 GDR1 + Cortana, I'm sure a lot of users or developers are flirting with Windows Phone's voice personal assistant Cortana, and in the World Cup I tested Cortana to predict that the German and Argentine races were of the subject. But as a developer, how do we integrate Cortana into the app, and today I'll give you a little time to talk about how to use voice command to integrate with Windows Phone 8.1 apps.
First of all to clear two noun voice command & Voice Command Definition is a VCD file, I believe that the development of Windows Phone 8.0 friends should have some understanding, through the registration of VC d file Windows Phone 8.0 application can implement voice command function, if you do not understand please read my previous article (here I am not too much to introduce 8.0 voice command of the duplicate content), Windows phone 8 Voice-Speech for Windows Phone 8 get a quick look at the pre-prep work for Windows Phone development voice features.
Simply speaking, the Windows Phone 8.0 Voice Command function is relatively simple, mainly through voice command Name to determine the pre-fabricated in a VCD file several commands.
Cortana provides stronger natural language recognition in Windows Phone 8.1 apps (Natural language recognition)
Of course, the grammars in the VCD file has also been expanded, and the difference between the two OS versions of the
http://schemas.microsoft.com/voicecommands/1.0 for Windows Phone 8.0 Voice Command and Cortana compatible.
http://schemas.microsoft.com/voicecommands/1.1 for widnows Phone 8.1 cortnan.
For more information, please refer to
Windows Phone 8.0:voice command element and attribute reference for Windows Phone 8Windows phone 8.1:voice command ele ments and Attributes
With the 8.0 and 8.1 vcd file attribute support Situation there is a major difference in the 8.1 VCD support phrasetopic this property.
The text said too abstract or paste out the code to everyone say it:
Here I mainly emphasize the Listenfor node and the Phrasetopic knot point. Note that the bracket {dictatedsearchterms} in the Listenfor node is the corresponding Phrasetopic node in the Label property. At the same time we can interpret phrasetopic as arbitrary content . Finally, you can get back to our app from Cortana.
<Voicecommandsxmlns= "http://schemas.microsoft.com/voicecommands/1.1"> <!--The CommandSet Name is used to programmatically access the CommandSet - <CommandSetXml:lang= "ZH-CN"Name= "Chinesecommands"> <!--The Commandprefix provides an alternative to your full app name for invocation - <Commandprefix>Microsoft documentation</Commandprefix> <!--The CommandSet Example appears in the global Help alongside your app name - <Example>Search constructors</Example> <CommandName= "Msdnsearch"> <!--The Command example appears in the Drill-down help page for your app - <Example>Search Constructors '</Example> <!--listenfor elements provide ways to say the command, including references to {phraselists} and {Phrasetopi CS} as well as [optional] words - <listenfor>Find {dictatedsearchterms}</listenfor> <listenfor>Search {dictatedsearchterms}</listenfor> <listenfor>Search {dictatedsearchterms}</listenfor> <listenfor>Check {dictatedsearchterms}</listenfor> <listenfor>Find {dictatedsearchterms}</listenfor> <!--Feedback provides the displayed and spoken text when your command is triggered - <Feedback>Find MSDN ...</Feedback> <!--Navigate Specifies the desired page or invocation destination for the Command - <NavigateTarget= "MainPage.xaml" /> </Command> <CommandName= "Msdnnaturallanguage"> <Example>I want to go to Windows Mobile Dev Center</Example> <listenfor>{Naturallanguage}</listenfor> <Feedback>Start MSDN ...</Feedback> <NavigateTarget= "MainPage.xaml" /> </Command> <PhrasetopicLabel= "Dictatedsearchterms"Scenario= "Search"> <Subject>Msdn</Subject> </Phrasetopic> <PhrasetopicLabel= "Naturallanguage"Scenario= "Natural Language"> <Subject>Msdn</Subject> </Phrasetopic> </CommandSet></Voicecommands>
Learn about the new VCD file, here I remind everyone, in fact, in the Windows Phone 8.0 app can also be compatible with Cortana's features , In 8.0 of the application, we only need to determine the operating system version and then select a different VCD file to register the system.
First of all, we need to put two versions of VCD files in the project to
Second, when registering the VCD file, according to the system version of the judgment can be.
/// <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 voidinstallvoicecommands () {Const stringWp80vcdpath ="Ms-appx:///voicecommanddefinition_8.0.xml"; Const stringWp81vcdpath ="Ms-appx:///voicecommanddefinition_8.1.xml"; Const stringChinesewp80vcdpath ="Ms-appx:///chinesevoicecommanddefinition_8.0.xml"; Const stringChinesewp81vcdpath ="Ms-appx:///chinesevoicecommanddefinition_8.1.xml"; Try { BOOLUsing81orabove = ((Environment.OSVersion.Version.Major >=8) && (Environment.OSVersion.Version.Minor >=Ten)); stringVcdpath = Using81orabove?Wp81vcdpath:wp80vcdpath; if(InstalledSpeechRecognizers.Default.Language.Equals ("ZH-CN", Stringcomparison.invariantcultureignorecase)) {Vcdpath= Using81orabove?Chinesewp81vcdpath:chinesewp80vcdpath; } Uri Vcduri=NewUri (Vcdpath); awaitVoicecommandservice.installcommandsetsfromfileasync (Vcduri); } Catch(Exception vcdex) {dispatcher.begininvoke ()={MessageBox.Show (String.Format (Appresources.voicecommandin Stallerrortemplate, Vcdex.hresult, vcdex.message)); }); } }
Finally in the application to get the user's voice input method, note that this is also required by the phrasetopic node of the Label name obtained.
/// <summary> ///Takes specific action for a retrieved Voicecommand name. /// </summary> /// <param name= "Voicecommandname" >The command name triggered to activate the application</param> Private voidHandlevoicecommand (stringvoicecommandname) { //Voice Commands can be typed to Cortana; When this happens, "Voicecommandmode" are populated with the //"TextInput" value. In these cases, we'll want to behave a little differently by not speaking back. BOOLTypedvoicecommand = (NavigationContext.QueryString.ContainsKey ("Commandmode") && (navigationcontext.querystring["Commandmode"] =="text")); stringPhrasetopiccontents =NULL; BOOLDosearch =false; Switch(voicecommandname) { Case "Msdnnaturallanguage": if(NavigationContext.QueryString.TryGetValue ("Naturallanguage", outphrasetopiccontents)&&!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 would 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", outphrasetopiccontents); Dosearch=true; Break; } if(Dosearch) {handlesearchquery (phrasetopiccontents, Typedvoicecommand); } }
The whole process is as simple as moving, and quickly add your app to Cortana to get your little friends to flirt.
More resources:
Quickstart:voice commands (XAML)
Speech for Windows Phone 8
QuickStart: Voice Commands (XAML)
SOURCE Download:
MSDN Voice Search for Windows Phone 8.1