This is an old revision of the document!
Voice Capabilities (How to make speech to text converter)
User voice will be converted into text and shown on Android Screen. As soon as a user say something, Android will recognize his/her voice and convert it into text. It will do it through RecognizerIntent. Here you don’t need any internet connection. It will work in Offline mode.
In the next java code, we are triggering an Intent named RecognizerIntent which asks for speech input and then sends it through speech recognizer. It does it through ACTION_RECOGNIZE_SPEECH. The results will be returned via activity results in onActivityResult(int, int, Intent). If request code is REQ_CODE_SPEECH_INPUT then corresponding text is written in output screen.
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-En"); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Hi speak something"); // Start the activity, the intent will be populated with the speech text startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
// This callback is invoked when the Speech Recognizer returns. // This is where you process the intent and extract the speech text from the intent. @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQ_CODE_SPEECH_INPUT && resultCode == RESULT_OK) { List<String> results = data.getStringArrayListExtra( RecognizerIntent.EXTRA_RESULTS); String spokenText = results.get(0); // Do something with spokenText } }
https://developer.android.com/training/wearables/apps/voice.html
Text-To-Speech in Android
Text-To-Speech (TTS)- Also known as “speech synthesis”, TTS enables your Android device to “speak” text of different languages.
The TTS engine that ships with the Android platform supports a number of languages: English, French, German, Italian and Spanish. Also, depending on which side of the Atlantic you are on, American and British accents for English are both supported.
The TTS engine needs to know which language to speak, as a word like “Paris”, for example, is pronounced differently in French and English.
In order to use this class, you need to instantiate an object of this class and also specify the initListener. Its syntax is given below −
TextToSpeech ttobj=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { } });
In this listener, you have to specify the properties for TextToSpeech object , such as its language ,pitch e.t.c. Language can be set by calling setLanguage() method. Its syntax is given below −
ttobj.setLanguage(Locale.UK);
Once you have set the language, you can call speak method of the class to speak the text. Its syntax is given below −
ttobj.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
https://android-developers.googleblog.com/2009/09/introduction-to-text-to-speech-in.html
https://www.tutorialspoint.com/android/android_text_to_speech.htm