コード例 #1
0
        /// <summary>
        /// Click event of stop button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async Task Stop_Clicked(object sender, EventArgs e)
        {
            isRecording = false;
            try
            {
                if (!isRecording)
                {
                    SwitchPlayOrStop(isRecording, true);

                    recorder.StopRecording();
                    // Request Speech REST API
                    speechRecognition.HttpRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(
                        @"https://speech.platform.bing.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US");
                    // Authenticate and get a JSON Web Token (JWT) from the token service.
                    //if (speechRecognition.Task == null || speechRecognition.Task.IsCompleted)
                    await speechRecognition.Authenticate(SPEECH_API_KEY);

                    // Send the request to Bing Speech API REST end point.
                    var displayText = "Could not detect, please try again!";
                    if (String.IsNullOrEmpty(recorder.WavFilePath))
                    {
                        await DisplayAlert("Message", displayText, "Cancel");

                        return;
                    }
                    speechRecognition.AudioFile = recorder.WavFilePath;
                    speechRecognition.SendRequest();
                    // Get response to get your transcribed text.
                    Newtonsoft.Json.Linq.JObject json = Newtonsoft.Json.Linq.JObject.Parse(speechRecognition.GetResponse());
                    //Get Display Text Values
                    if (json.Count > 1)
                    {
                        displayText = json.Value <string>("DisplayText");
                        // Translate the display text
                        tranlation.Key    = GOOGLE_TRANSLATE_API;
                        tranlation.Text   = displayText;
                        tranlation.Target = "ja";
                        await tranlation.Translate();

                        // Get translated text from response
                        json = Newtonsoft.Json.Linq.JObject.Parse(tranlation.StringJsonReponse);
                        json = json.Value <Newtonsoft.Json.Linq.JObject>("data");
                        var translatedText = (from j in json["translations"] select(string) j["translatedText"]).First <string>().ToString();
                        // Get pronunciation for translated text
                        var pronunciation = await tranlation.CallWatsonPronunciationAPI(translatedText);

                        json = Newtonsoft.Json.Linq.JObject.Parse(pronunciation);

                        TranslatedText.Text   = translatedText;
                        TranslatedText.Detail = json.Value <string>("pronunciation");
                    }
                    RecordedText.Text = displayText.ToString();
                    SwitchPlayOrStop(isRecording, false);
                }
            }
            catch (ArgumentException ex)
            {
                await DisplayAlert("Error", ex.Message, "Cancel");
            }
        }