Beispiel #1
0
        public void RecognizeSpeech(NSUrl url)
        {
            var recognizer = new SFSpeechRecognizer(new NSLocale("en_US"));

            // Is the default language supported?
            if (recognizer == null)
            {
                return;
            }

            // Create recognition task and start recognition
            var request = new SFSpeechUrlRecognitionRequest(url);

            recognizer.GetRecognitionTask(request, (SFSpeechRecognitionResult result, NSError err) =>
            {
                // Was there an error?
                if (err != null)
                {
                    var alertViewController = UIAlertController.Create("Error",
                                                                       $"An error recognizing speech ocurred: {err.LocalizedDescription}",
                                                                       UIAlertControllerStyle.Alert);
                    PresentViewController(alertViewController, true, null);
                }
                else
                {
                    if (result.Final)
                    {
                        InvokeOnMainThread(() =>
                        {
                            SpeechToTextView.Text = result.BestTranscription.FormattedString;
                        });
                    }
                }
            });
        }
Beispiel #2
0
        public string RecognizeVoice(string path, Editor editor)
        {
            SFSpeechRecognizer recognizer = new SFSpeechRecognizer(NSLocale.CurrentLocale);

            // Is the default language supported?
            if (recognizer == null)
            {
                // No, return to caller
                return("recognizer is null");
            }

            // Is recognition available?
            if (!recognizer.Available)
            {
                // No, return to caller
                return("recognizer is not available");
            }
            isTranslatingDone = false;
            NSUrl url = new NSUrl(path, false);
            SFSpeechUrlRecognitionRequest request = new SFSpeechUrlRecognitionRequest(url);
            SFSpeechRecognitionTask       sptask  = recognizer.GetRecognitionTask(request, (SFSpeechRecognitionResult result, NSError err) =>
            {
                if (err != null)
                {
                    resultString      = err.Description;
                    isTranslatingDone = true;
                    editor.Text       = resultString;
                }
                else
                {
                    if (result != null)
                    {
                        if (result.Final)
                        {
                            resultString      = result.BestTranscription.FormattedString;
                            isTranslatingDone = true;
                            // call back??
                            editor.Text = resultString;
                        }
                    }
                }
            });

            return("cannot recognize");
        }