Jun 152013
 
 June 15, 2013  Posted by at 1:58 pm Uncategorized, WP  Add comments

Bits and pieces from my slides and code from my session on WP8 at Norwegian Developer Conference 2013

 

Speech is a rather neat feature of Windows Phone, and we can now do more than just launch an application. Integrating speech in the app is easy, and allows the user one more way to interact with the application. To use voice commands hold down the windows ‘button’ until you are prompted to make request a command.

Three main concepts in speech:

Speech_wp8

 

Note that speech is also accessible from the lock screen – but this has to be enabled in settings (settings- speech).

Voice commands are for sending commands to an app with pre-specified arguments (the so-called phrase list).

Speech recognition is used from within the app.

Text to speech will read out specified text in languages supported and chosen. Default language is locale.

 

The steps

Speech_wp8_2

Let’s walk through the steps:

1. Capability

2. Namespace

using Windows.Phone.Speech.Synthesis;
using Windows.Phone.Speech.VoiceCommands;
using Windows.Phone.Speech.Recognition;

3. Add VCD file

Right click on project, Add new file. Select VCD – Voice Command Definition file. The file will have an example in it.

[sourcecode language=”XML”]
<?xml version="1.0" encoding="utf-8"?>
<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.0">
<CommandSet xml:lang="en-US">
<CommandPrefix>Barstock manager</CommandPrefix>
<Example> add one beer </Example>
<Command Name="AddItem">
<Example> add one beer</Example>
<ListenFor> [Add] {number} {type} </ListenFor>
<Feedback> Adding a new item to barstock… </Feedback>
<Navigate />
</Command>
<PhraseList Label="number">
<Item> 1 </Item>
<Item> 2 </Item>
<Item> 3 </Item>
</PhraseList>
<PhraseList Label="type">
<Item> beer </Item>
<Item> wine </Item>
<Item> soda </Item>
</PhraseList>
</CommandSet>
</VoiceCommands>

[/sourcecode]

CommandSet allows you to set language

CommandPrefix is what you want the user to call the app,

Command is what the user wants the app to do.

Example is an example provided to the user so they know how they can call the command

ListenFor is what we expect the user to say. Square brackets are optional words, and brackets are arguments passed to the command.

The arguments the user can use is added in a PhraseList. this list can also be updated dynamically.

4. Register Service

Do this when the app starts up, the app has to to run at least once before commands can be used.

Example code:

await VoiceCommandService.InstallCommandSetsFromFileAsync(new Uri("ms-appx:///ex.xml"));

 

5. Handle invocation

Once a command is called you need to handle invocation. Here is an example, notice that we use querystring property on the navigationcontext to get our arguments and command name.

[sourcecode language=”csharp”]
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
// New or tombstone?
if (e.NavigationMode != System.Windows.Navigation.NavigationMode.New) return;
// Voice command?
if (!NavigationContext.QueryString.ContainsKey("voiceCommandName")) return;

var voiceCommandName = NavigationContext.QueryString["voiceCommandName"];

if (voiceCommandName != "AddItem") return;
var nr = NavigationContext.QueryString["number"];
var type = NavigationContext.QueryString["type"];

NavigationService.Navigate(new Uri("/SpeechPage.xaml?number=" + nr + "&type=" + type, UriKind.RelativeOrAbsolute));
}
[/sourcecode]

Providing feedback by using text to speech:

await new SpeechSynthesizer().SpeakTextAsync("Say this");

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

What is 3 + 6 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)