Esempio n. 1
0
 public void recognize(
     Locale language,
     RecognitionCallback recognitionCallback,
     ExceptionCallback exceptionCallback,
     ProgressCallback progressCallback) {
   if (!loaded) {
     exceptionCallback.onException(
         new IllegalStateException("Speech recognizer not initialized yet."));
   } else if (!isLanguageAvailable(language)) {
     exceptionCallback.onException(
         new IllegalStateException("Speech recognizer not available for this language."));
   } else if (this.recognitionCallback != null || this.exceptionCallback != null) {
     exceptionCallback.onException(new IllegalStateException("There is an ongoing recognition."));
   } else {
     this.recognitionCallback = recognitionCallback;
     this.exceptionCallback = exceptionCallback;
     this.progressCallback = progressCallback;
     final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, compatibleLanguageCode(language));
     intent.putExtra(
         "android.speech.extra.EXTRA_ADDITIONAL_LANGUAGES",
         new String
             [] {}); // TODO This is undocumented but necessary. See
                     // https://productforums.google.com/forum/#!topic/websearch/PUjEPmdSzSE/discussion
     intent.putExtra(
         RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
     intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, progressCallback != null);
     intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
     recognizer.startListening(intent);
   }
 }
Esempio n. 2
0
 @Override
 public void onError(int error) {
   switch (error) {
     case SpeechRecognizer.ERROR_AUDIO:
       if (exceptionCallback != null)
         exceptionCallback.onException(new Exception("Audio recording error."));
       break;
     case SpeechRecognizer.ERROR_CLIENT:
       if (exceptionCallback != null)
         exceptionCallback.onException(new Exception("Other client side errors."));
       break;
     case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
       if (exceptionCallback != null)
         exceptionCallback.onException(new Exception("Insufficient permissions."));
       break;
     case SpeechRecognizer.ERROR_NETWORK:
       if (exceptionCallback != null)
         exceptionCallback.onException(new Exception("Other network related errors."));
       break;
     case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
       if (exceptionCallback != null)
         exceptionCallback.onException(new Exception("Network operation timed out."));
       break;
     case SpeechRecognizer.ERROR_NO_MATCH:
       if (recognitionCallback != null) recognitionCallback.onRecognitionDone("");
       break;
     case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
       if (exceptionCallback != null)
         exceptionCallback.onException(new Exception("RecognitionService busy."));
       break;
     case SpeechRecognizer.ERROR_SERVER:
       if (exceptionCallback != null)
         exceptionCallback.onException(new Exception("Server sends error status."));
       break;
     case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
       if (recognitionCallback != null) recognitionCallback.onRecognitionDone("");
       break;
   }
   recognitionCallback = null;
   exceptionCallback = null;
 }