private async void Speak(SpeechMessage speech) { var service = new Speech(); await this.dispatcher.RunAsync( CoreDispatcherPriority.Normal, async () => { try { if (speech.Action != null && speech.Action.Equals("STOP")) { this.player?.Stop(); } else { service.Speak(this.player, speech); } } catch (Exception e) { await this.SendResult(new ResultMessage(speech) { Result = e.Message, ResultId = e.HResult }); } }); }
private async void Recognize(SpeechRecognitionMessage speech) { if (this.speechService == null) { this.speechService = new Speech(); this.speechService.SpeechStatusChanged += (sender, args) => { this.appSettings.IsListening = args.Status == SpeechStatus.Listening; }; } await this.dispatcher.RunAsync( CoreDispatcherPriority.Normal, async () => { try { if (speech.Action != null && speech.Action.Equals("STOP")) { this.speechService.Stop(); return; } var expectedConfidence = speech.Confidence ?? (int)SpeechRecognitionConfidence.Medium; var recognizeText = default(RecognizedSpeech); var confident = false; var timeout = DateTime.UtcNow.AddMilliseconds(speech.Ms ?? 0); while (!confident && (speech.Ms == null || speech.Ms == 0 || DateTime.UtcNow < timeout)) { // consider timeout here Debug.WriteLine("recognizing..."); recognizeText = await this.speechService.Recognize(speech.Message, speech.UI); Debug.WriteLine("end recognizing..."); if (recognizeText.status != SpeechRecognitionResultStatus.Success) { break; } confident = expectedConfidence >= recognizeText.confidence; } var status = recognizeText.status == SpeechRecognitionResultStatus.Success ? recognizeText.index : (recognizeText.status == SpeechRecognitionResultStatus.UserCanceled ? 0 : -(int)recognizeText.status); if ((recognizeText.status == SpeechRecognitionResultStatus.Unknown || recognizeText.status == SpeechRecognitionResultStatus.Success) && !confident && (speech.Ms != null && speech.Ms != 0 || DateTime.UtcNow >= timeout)) { status = 0; // timeout or cancelled } await this.SendResult( new SpeechResultMessage(speech) { Result = recognizeText.text, ResultId = status, Action = recognizeText.action, Value = recognizeText.confidence }); } catch (Exception e) { await this.SendResult(new ResultMessage(speech) { Result = e.Message, ResultId = e.HResult }); } }); }