Speech to Text without Dialog
suggest changeThe following code can be used to trigger speech-to-text translation without showing a dialog:
public void startListeningWithoutDialog() { // Intent to listen to user vocal input and return the result to the same activity. Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); // Use a language model based on free-form speech recognition. intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5); intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, appContext.getPackageName()); // Add custom listeners. CustomRecognitionListener listener = new CustomRecognitionListener(); SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(appContext); sr.setRecognitionListener(listener); sr.startListening(intent); }
The custom listener class CustomRecognitionListener
used in the code above is implemented as follows:
class CustomRecognitionListener implements RecognitionListener { private static final String TAG = "RecognitionListener"; public void onReadyForSpeech(Bundle params) { Log.d(TAG, "onReadyForSpeech"); } public void onBeginningOfSpeech() { Log.d(TAG, "onBeginningOfSpeech"); } public void onRmsChanged(float rmsdB) { Log.d(TAG, "onRmsChanged"); } public void onBufferReceived(byte[] buffer) { Log.d(TAG, "onBufferReceived"); } public void onEndOfSpeech() { Log.d(TAG, "onEndofSpeech"); } public void onError(int error) { Log.e(TAG, "error " + error); conversionCallaback.onErrorOccured(TranslatorUtil.getErrorText(error)); } public void onResults(Bundle results) { ArrayList<String> result = data .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); txtSpeechInput.setText(result.get(0)); } public void onPartialResults(Bundle partialResults) { Log.d(TAG, "onPartialResults"); } public void onEvent(int eventType, Bundle params) { Log.d(TAG, "onEvent " + eventType); } }
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents