/** * Initializes the ASR and TTS engines. @Note: It is not possible to do it in a constructor * because we cannot get the context for recognition before oncreate */ public void initializeAsrTts() { // Initialize text to speech myTts = TTS.getInstance(getApplicationContext()); // Initialize the speech recognizer createRecognizer(getApplicationContext()); }
/** * Synthesizes a prompt. If it is not possible, it creates a toast with the content of the message */ private void playPrompt(String prompt) { try { myTts.speak(prompt, "EN"); } catch (Exception e) { Toast.makeText(this, prompt, Toast.LENGTH_LONG).show(); Log.e(LOGTAG, "English not available for TTS, default language used instead"); } }
/** * Provides feedback to the user (by means of a Toast and a synthesized message) when the ASR * encounters an error * * <p>If the error is a nomatch or no input, then interprets the field again (asks the user again * for the same information) If not, the dialog is stopped. * * <p>CHANGED WITH RESPECT TO FORMFILLLIB (chapter 5) Changes: - before starting to interpret the * current field again, it checks whether it has been filled in the GUI */ @Override public void processAsrError(int errorCode) { String errorMessage; switch (errorCode) { case SpeechRecognizer.ERROR_AUDIO: errorMessage = "Audio recording error"; break; case SpeechRecognizer.ERROR_CLIENT: errorMessage = "Client side error"; break; case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS: errorMessage = "Insufficient permissions"; break; case SpeechRecognizer.ERROR_NETWORK: errorMessage = "Network related error"; break; case SpeechRecognizer.ERROR_NETWORK_TIMEOUT: errorMessage = "Network operation timeout"; break; case SpeechRecognizer.ERROR_RECOGNIZER_BUSY: errorMessage = "RecognitionServiceBusy"; break; case SpeechRecognizer.ERROR_SERVER: errorMessage = "Server sends error status"; break; case SpeechRecognizer.ERROR_NO_MATCH: errorMessage = nomatch; // Error message obtained from the VXML file break; case SpeechRecognizer.ERROR_SPEECH_TIMEOUT: errorMessage = noinput; // Error message obtained from the VXML file break; default: errorMessage = "ASR error"; break; } try { myTts.speak(errorMessage, "EN"); } catch (Exception e) { Log.e(LOGTAG, "English not available for TTS, default language used instead"); } if (errorCode == SpeechRecognizer.ERROR_NO_MATCH || errorCode == SpeechRecognizer.ERROR_SPEECH_TIMEOUT) { // If there is a nomatch or noinput, interprets the field again. if (form.getField(currentPosition).isFilled()) moveToNextField(); // Necessary for multimodal apps // in which the field can be filled in the GUI while the oral dialog tries to prompt again for // the information else interpretCurrentField(); } else { // If there is an error, shows feedback to the user and writes it in the log Log.e(LOGTAG, "Error: " + errorMessage); Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show(); } }
// TODO public void shutdownTts() { myTts.shutdown(); }