To play off of my last post on Text to Speech in Windows Phone, I thought I would continue the theme of cartoon characters. So, anyone out there heard of Felix the Cat? Felix the Cat was a funny cartoon character in the silent film era. He always found himself in a fix and needed to resort to his bag of tricks. Well, let’s jump into the Windows Phone SDK bag of tricks and see how we can send voice commands to our Windows Phone 8 application.
Windows Phone 7.x introduced simple voice commands such as “Open Ebay”, “Call Ed Glogowski”, “Find food in Apple Valley”, “Text John Cannon” or even “Note It is my wife’s birthday on Friday”.
Windows Phone 8 has given developers the option to extend the voice commands to call directly into their application.
Let’s build on the solution we built in my previous post, Text to Speech in Windows Phone, and add voice commands to open up the app to various pivot locations on the main page, text and voice. In fact, if it is the voice pivot, lets put the command text into a TextBlock field, hear the text automatically and allow the user to hear the text again in the default language by pressing a button. How does that sound?
Once you have our old solution loaded up and ready, the first thing we need to do is create a new Voice Command Definition file to our project. To do this, right-click on project for a context menu, select Add and then select New Item.
From the New Item Dialog, select Voice Command Definition from the Installed/Visual C# tree. Name it something like VoiceDef.xml.
Now, let’s open up the voice command definition file. Open the file. If you skip down to the CommandSet element, you will see that the first two elements are CommandPrefix and Example. CommandPrefix is the prefix to your voice command identifying your application. The Example element is a very simple example that gets displayed to the user on how to use the voice command with the prefix for your application. So for now, lets put in “Speak App” for the CommandPrefix and “Go to text…” for the Example. It should look something like this:
Okay, now we need to add a couple commands that will allow us to actually call into our app. Go ahead and remove all the commands in the CommandSet element and add the following:
Now, lets talk a little about what we just added. First, each command has a unique name, so we added two commands: GotoText and GotoVoice. Probably don’t need to call out the obvious, but GotoText will go to our MainPage and go to the Text pivot item, while the GotoVoice command will go to our MainPage and select the Voice pivot item.
The Example elements specify what is displayed when the user prompts for the list of voice commands for an application.
The ListenFor elements are where the magic happens. The ListenFor element defines what it is listening for after the CommandPrefix. You will see that for the Command “GotoText”, we have one ListenFor element that checks to see if “go to text” was spoken after the Prefix. The other ListenFor is almost the same, but has a “[and]”. Any word in ’s is considered optional. So, in this case, both of these voice commands would fire this command: “Speak App go to text” and “Open Speak App and go to text”. Optional words are used to allow the user to speak more naturally. There are other parameters you can use within the ListenFor element, but I won’t go into those right now.
The Feedback element is used to display to the user after the command has been recognized and is attempting to load your app.
The Navigate element is optional. You will see that I did not include one in the first command, but did in the second. For the first command, in our apps design, we just simply want to go to the main page text pivot. Since that is what happens when you start the app anyway, there isn’t much to do. However, you will notice that I did put one in for the second command. Now, as you can see, we could put a URI to any page in our app, but again, by design of the app, we are choosing to go to the voice pivot on the main page. So, what I did was use the URI to the main page and included a parameter for the pivot we want to display on load.
Okay, that was a lot of talking, or should I say typing, can we see some cool stuff yet? Well, not quite. In order to use the new voice commands we just created, we will need to install the file when the app starts up. To do this, lets create a new function, SetupVoiceCommands() and call it from the constructor for MainPage. The function will simply install the command set from file via URI. Which is awesome, because that means we could load it from any URI and at almost anytime in the application. This might be useful for applications like games where the user gets more commands based on how far they are in the game. The code should look something like the following:
Now let’s make a couple of UI changes. First, lets update our voice pivot in MainPage.xaml. Give it a name so that we can reference it easily in the code behind. I called it “pivotMain”. Add a TextBlock and a Button. We will use the TextBlock to display the recognized command and a Button to have the application speak the text in the TextBlock to us. I named the TextBlock “tVoice”. So, the XAML for the Pivot control should look like this:
You can see that I added a copy click event called SpeakText that basically is a copy and past from the other event handlers (I know, create a common function for the redundant code. But this is only a demo) and it looks like this:
Now, the only thing left to do is to take care of what happens when we get to the page after speaking the voice command. All we do is override OnNavigateTo() to handle what was passed into the URI for that page. You will see that all we do is check to see if there is a “pivot” parameter. If there is one, we check to see if it’s value is “voice”. If it is, we set the pivot to the Voice pivot.
Next, we check for a “reco” parameter that the voice command sends back with the text that it found for the command. Now, with that, we are free to do whatever we want with it. In our case, we are going to put it into the TextBlock and then use Text to Speech to play the command back to us. Well, with a little extra text for fun. See the code below:
Now, run the app. Once it is loaded. Tap on the Windows button to go to the Start Screen., From there, hold the Windows key down and when prompted, say, “Speak App go to voice.” Your program should have loaded to the “voice” pivot and spoke to you the following: “You spoke to me. Here is what you said Speak App go to voice.”
So, as you can see, there might have been a lot of text above explaining what we did, but actually, there was very little code. This should be exciting to you as a developer. Just think how easy it is to add voice commands to your Windows Phone and provide much quicker access to your application and it’s functionality.
Hope you enjoyed the article.