//参数为文档中的Operation public static void SendCmd(string str) { HandleModel handle = new HandleModel(); handle = StringHandle.MessageHandle(handle, str); string msg = JsonConvert.SerializeObject(handle); Command command = new Command { Flag = "1", Msg = msg }; //所发送的Msg,{"FullName":"我的电脑","FileTyp":null,"Name":"我的电脑","Lable":"我的电脑"}"} string message = JsonConvert.SerializeObject(command); App.SendData(message, App.socketCmd); }
private async void Voice(object sender, RoutedEventArgs e) { voice.IsEnabled = false; using (SpeechRecognizer speechRecognizer = new SpeechRecognizer()) { try { //编译所有语法协定 SpeechRecognitionCompilationResult compilationResult = await speechRecognizer.CompileConstraintsAsync(); if (compilationResult.Status == SpeechRecognitionResultStatus.Success) { //开始语音识别 var recogResult = await speechRecognizer.RecognizeWithUIAsync(); if (recogResult.Status == SpeechRecognitionResultStatus.Success) { info = recogResult.Text; HandleModel handle = new HandleModel(); handle = StringHandle.MessageHandle(handle, info); string msg = JsonConvert.SerializeObject(handle); Command command = new Command { Flag = "1", Msg = msg }; if (handle.Typ != "") { string sendMsg = JsonConvert.SerializeObject(command); Debug.WriteLine("语音发送的命令:" + sendMsg); TextToSpeech("正在为你" + info); App.SendData(sendMsg, App.socketCmd); voice.IsEnabled = true; } else { handle.Typ = "Search"; //handle.Msg = speechContent.Text; string uriMsg = string.Format("{0}{1}", App.Search["百度"], System.Net.WebUtility.UrlEncode(info)); Uri uri = new Uri(uriMsg); handle.Msg = uriMsg; string msg1 = JsonConvert.SerializeObject(handle); command.Msg = msg1; string sendMsg = JsonConvert.SerializeObject(command); Debug.WriteLine("语音发送的命令:" + sendMsg); webView.Visibility = Visibility.Visible; webView.Navigate(uri); TextToSpeech("指令无效即将使用百度搜索"); App.SendData(sendMsg, App.socketCmd); voice.IsEnabled = true; } } //语音识别失败 if (recogResult.Status != 0) { voice.IsEnabled = true; } } } catch (Exception ex) { Debug.WriteLine("语音异常:" + ex.Message); //await new MessageDialog("语音异常:" + ex.Message).ShowAsync(); } } }