Professional Documents
Culture Documents
Cosc 5/4730: Text To Speech and Speech To Text
Cosc 5/4730: Text To Speech and Speech To Text
Text to Speech
And Speech To Text
Android
TEXT TO SPEECH
Text to Speech
• In Android 1.6+ there is a native Text-to-speech built into the
Android OS.
– In 4.X+, Settings-> Language and Input -> Text-to-speech output
– You can use the “Listen to an example” to see how it works.
How it works.
• The Text-to-Speech (TTS) uses a the Pico engine
– It sends the “speech” to the audio output.
• There is only one TTS engine and it is share across all the
activities on the device.
– Other activities maybe using it
– The user may have overridden the settings in the preferences as well.
Using the TTS
• First we need to check if the TTS engine is available.
– We can do this with a Intent to with ACTION_CHECK_TTS_DATA
• Using startActivityForResult, we then find out if the TTS engine is working and
avialable.
Android.speech.tts
• To use the TTS we need get access to it using the constructor
• TextToSpeech(Context context, TextToSpeech.OnInitListener
listener)
– The constructor for the TextToSpeech class.
mTts = mTts = new TextToSpeech(this, this);
– First this, use the context of our application
– Likely US-EN
– Second this, the listener.
• … Activity implements OnInitListener
– @override public void onInit(int status)
OnInitListener
• onInit(int status)
– Called to signal the completion of the TextToSpeech engine
initialization.
– Status is either
• TextToSpeech.SUCCESS
– You can use it.
– or
• TextToSpeech.ERROR
– Failure, you can’t use it.
Using the TTS
• To have it speak words
– speak(String text, int queueMode, HashMap<String, String> params)
• To stop, call stop()
• Shutdown() to release everything
Example
• API 20 and below
– mTts.speak(“Test”, TextToSpeech.QUEUE_ADD, null);
• TextToSpeech.QUEUE_FLUSH will remove all of this app's unspoken and start
with current.
• API 21+
– mTts.speak(“Test”, TextToSpeech.QUEUE_ADD, null, utteranceId);
• Where utteranceID is a string, like "myid"
– Can be used for the setOnUtteranceProgressListener(UtteranceProgressListener))
– You should hear the word test spoken.
Other methods.
• You can change the pitch and speech rate with
– setPitch(float pitch)
– setSpeechRate(float speechRate)
• To find out if “it” is still speaking
– Boolean isSpeaking()
• To have the speech written to a file
– synthesizeToFile(String text, HashMap<String, String> params, String
filename)
• Remember permission for writing to the file system.
Note
• In the OnPause() method
– You should put at least a stop() call
– You app has lost focus
Example code
• text2speech example in github
– Simple text box and button. Type in the words you want to speak
and then press play.
SPEECH TO TEXT
Speech To Text
• Like Text to speech, we are going to call on another Google's
voice recognition software.
– Android.speech package
– The simple version uses an intent and there is a dialog box for the
users to know when to speech.
• RecognizerIntent
– With a onActivityResult
• http://www.ispeech.org/
References
• http://developer.android.com/resources/samples/ApiDemos/s
rc/com/example/android/apis/app/TextToSpeechActivity.html
• http://developer.android.com/reference/android/speech/Spee
chRecognizer.html
• http://developer.android.com/resources/samples/ApiDemos/s
rc/com/example/android/apis/app/VoiceRecognition.html
• http://stackoverflow.com/questions/6316937/how-can-i-use-
speech-recognition-without-the-annoying-dialog-in-android-
phones
Q&A