Hello developer, welcome to my new blog in this blog we would learn how to add a text-to-speech feature to our Android App, let's get started without further ado.
Talk - Text to Speech engine ( T T S )
We would be using TTS ( Text-to-Speech ) engine provided by the Android operating system which converts the text to spoken words. TTS technology is being used in Android smartphones, and tablets providing functionalities in Accessibility, Navigation Apps, voice assistance and many more.
In Android we have TextToSpeech
class helps us interact with the TTS engine which provides us with different methods to generate Spoken words from the text.
Some features of TTS:-
It can convert textual Strings or Sentences into Spoken words output.
It provides us with various controls like we can change the pitch, speed and volume of the output.
It has multiple languages support: we can provide languages using
locale
.It has Queuing mechanism, where we can queue multiple texts.
Let's dive into the code :
Step 1: Setup your new Android Project
Create a new Android Project, we would be implementing this Project using Java. I am assuming you are familiar with creating Android Projects and basic stuff. Jump to step 2 ;
Step 2: Coding + Understanding
- Activity must implement
TextToSpeech.OnInitListener
Interface:
In your Activity or Fragment where you will be creating the instance of TextToSpeech, it should implement TextToSpeech.OnInitListener
Interface which provides the callback mechanism for receiving the initialization status of the Text-Two-Speech engine. This interface has only one method void oninit(int status)
which is called just after the TextToSpeech engine finishes its initialization and returns the status code.
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
` }
}
Add necessary permissions to
AndroidManifest.xml
file :<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
The
INTERNET
permission is required to download the TextToSpeech engine data and theWAKE_LOCK
permission is required to not let the device sleep while speaking.Creating an Instance of TextToSpeech + Coding
[ comments are added to the code carrying explanations ]
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// creating an instance of the TTS engine
TextToSpeech tts = new TextToSpeech(this, this);
/* I am using viewBinding , speakButton is the id of
the button , I am applying setOnClickListener on that
as I want it to speak the text when button is clicked .
*/
binding.speakButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// setting the text to speak
String text = "Like this blog , if you loved it!";
// Calling the speak method on the tts object created above
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
});
}
speak() method takes the text (which is to be spoken ) as the first argument; the second parameter queueMode
which can be TextToSpeech.QUEUE_FLUSH
OR TextToSpeech.QUEUE_ADD
.
When using TextToSpeech.QUEUE_FLUSH
any pending speech synthesis is interrupted and replaced with the new one. The TTS engine stops any ongoing speech and starts speaking the new speech.
When using TextToSpeech.QUEUE_ADD
would add the text to the synthesis queue, allowing the TTS engine to speak them one by one without interruption.
And the third parameter we are giving as null is utteranceid
as we do not want to assign any specific identifier to the speech requests.
receiving the CallBack
onInit
is called just after the TextToSpeech engine finishes its initialization and returns the status code.
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
// adding the language
int result = tts.setLanguage(Locale.English);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
/* Language data is missing or the language is not supported.
Handle the error here. */
} else {
/* Initialization successful.
you can add the code for speed , pitch change here
*/
}
} else {
/* TTS initialization failed.
Handle the error here. */
}
}
overriding
onDestroy( )
onDestroy
is an activity lifecycle callback method, here we would release all the memory or resources occupied by the TextTwoSpeech engine as the Activity gets destroyed@Override protected void onDestroy() { super.onDestroy(); if (tts != null) { // releasing all the occupied resources tts.stop(); tts.shutdown(); } }
Thats all !! You need to do, this for adding a Text to Speech in your Android App , if you have any doubts leave it in the comment I would be glad to answer it .
If you loved the blog do like it and share it!
Thank you!!