In my current Windows Phone 8 project WorkTime I used the very nice new feature of the Windows Phone 8 speech recognition Api. With Windows Phone 7 you have to handle with the Bing translation services, but now you have a capsulated API in the Windows.Phone.Speech.Recognition namespace. Here is a sample code which uses the default Dictation grammar for Recognition. It is also possible to define your own grammar (expected words) by code or using a SRGS grammar file.
W3C Standard definition: http://www.w3.org/TR/speech-grammar/
Microsoft MSDN documentation: http://msdn.microsoft.com/en-us/library/hh361633(v=office.14).aspx
SpeechRecognizerUI speechRecognizer;
public TimeSheetEntryPage()
{
InitializeComponent();
this.BindingValidationError += TimeSheetEntryPageBindingValidationError;
this.speechRecognizer = new SpeechRecognizerUI();
}
void TimeSheetEntryPageBindingValidationError(object sender, ValidationErrorEventArgs e)
{
this.HandleBindingValidationError(sender, e);
}
#region Events
private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
var vm = DataContext as TimeSheetEntryDetailViewModel;
if (vm != null)
{
vm.GoBackCommand.Execute(null);
}
}
#endregion
private async void Search_ActionIconTapped(object sender, EventArgs e)
{
this.speechRecognizer.Recognizer.Grammars.Clear();
// Use the short message dictation grammar with the speech recognizer.
speechRecognizer.Recognizer.Grammars.AddGrammarFromPredefinedType("message", SpeechPredefinedGrammar.Dictation);
await speechRecognizer.Recognizer.PreloadGrammarsAsync();
var sr = new SpeechRecognizerUI();
sr.Settings.ListenText = AppResources.VoiceListenTextDescription;
sr.Settings.ReadoutEnabled = true;
sr.Settings.ShowConfirmation = true;
try
{
// Use the built-in UI to prompt the user and get the result.
SpeechRecognitionUIResult recognitionResult = await sr.RecognizeWithUIAsync();
if (recognitionResult.ResultStatus == SpeechRecognitionUIStatus.Succeeded)
{
// Output the speech recognition result.
DescriptionTextBox.Text = recognitionResult.RecognitionResult.Text;
}
}
catch (Exception ex)
{
//TODO change
MessageBox.Show(ex.Message);
}
}
But there is an issue with the Speech recognition API, if you localize the app title with a C++ resource-only DLL.
If you use this way of localization for your app title you get an error message from the Speech Recognition API “We're sorry, but we can't access the network right now” ?!? (costs me several hours to find out this correlation)
But if you remove the reference to the AppResLib.dll it works properly.
Keine Kommentare:
Kommentar veröffentlichen