It’s a brand new day so let us learn something new. We are going to look into the Android Text to Speech (TTS).
You might be already familiar with this feature. Even if you are not worry not, today we will see how we can implement this feature in an application.
Before diving right into the code let us first understand the Android Text to Speech itself with a little introduction.
Quick Navigation
What is Android Text to Speech?
Android text to speech is a very powerful feature in your android device. It allows an Android application to convert any written text into spoken words. It is mainly for accessibility feature in the Android System. But you are free to use it for any task as you please.
For instance, you can convert this text “Hey there this is a tutorial from techenum!” to a perfectly understandable audio output. And what’s good is that the process isn’t event too difficult to understand.
If you have basic knowledge in building an Android Application, you’re good to go. So, in this blog post we will guide you through the steps required to implement TTS in your Android application.
Things we need before starting
Here are a list of things that are absolutely necessary before we start to implement Android TTS in the application.
- Familiarity with
Kotlin
and Android Development - Free time to implement and debug
Without further ado let’s get started.
Step 1: Create TextToSpeechManager.kt
class
First, create a new class in your project named: TextToSpeechManger.kt
. We are going to build the Android TTS feature within this class.
class TextToSpeechManager private constructor() {
}
Code language: Kotlin (kotlin)
The code above is just an empty class named TextToSpeechManager
, which has an empty constructor.
Step 2: Initialize TTS
if it is supported
Before we proceed we have to check if the Android system supports TTS. We can do this by simply checking the status code while initializing the TextToSpeech
class provided by Android.
To do so let us add a method named init()
in the newly created class.
class TextToSpeechManager private constructor() {
companion object {
val instance = TextToSpeechManager()
fun init(context: Context) {
instance.tts = TextToSpeech(context) { status ->
// check if initialization successful
if (status != TextToSpeech.SUCCESS) {
throw Exception("Could not initialize.")
}
}
}
}
}
Code language: Kotlin (kotlin)
In the code above, we created a companion object
and added a init() method. Also, we have added checks to see if the initialization was successful.
If there are any issues during the initialization, the app will crash. You can easily change this behavior, but it is out of the scope of this tutorial to do so.
I hope it is clear up to this point, as it is pretty straight forward.
Step 3: Add language and Speech Rate
Now that we have initialized out TTS engine. Let us change some default configuration before proceeding.
Create another function named setLocaleAndSpeechRate()
inside TextToSpeechManager
class. The code block for this method is provided below.
private fun setLocaleAndSpeechRate(locale: Locale, speechRate: Float) {
if (tts.voice.locale != locale) {
val languageStatus = tts.isLanguageAvailable(locale)
if (languageStatus == TextToSpeech.LANG_MISSING_DATA
|| languageStatus == TextToSpeech.LANG_NOT_SUPPORTED
) {
throw Exception("The language $locale is not supported.")
}
}
tts.language = locale
tts.setSpeechRate(speechRate)
}
Code language: Kotlin (kotlin)
In the code above we pass locale
and speechRate
from somewhere else. It will be covered in the next step.
Step 4: Convert text to audio output.
Now that we have prepared all the necessary methods for our TTS. Let us create one more method inside TextToSpeechManager
class by the name speak()
. This method will take a String as it’s argument.
Here is the code for the method.
fun speak(text: String, locale: Locale) {
setLocaleAndSpeechRate(locale, 1.0f)
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null)
}
Code language: Kotlin (kotlin)
In the code above we just set the locale and speech rate and then request android system to speak out our text.
Step 5: Complete source code for TextToSpeechManger
class
Here is the complete source code for the TextToSpeechManger
class. By this point your class should look something like this:
class TextToSpeechManager private constructor() {
private lateinit var tts: TextToSpeech
companion object {
val instance = TextToSpeechManager()
fun init(context: Context) {
instance.tts = TextToSpeech(context) { status ->
// check if initialization successful
if (status != TextToSpeech.SUCCESS) {
throw Exception("Could not initialize.")
}
}
}
}
fun speak(text: String, locale: Locale) {
setLocaleAndSpeechRate(locale, 1.0f)
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null)
}
private fun setLocaleAndSpeechRate(locale: Locale, speechRate: Float) {
if (tts.voice.locale != locale) {
val languageStatus = tts.isLanguageAvailable(locale)
if (languageStatus == TextToSpeech.LANG_MISSING_DATA
|| languageStatus == TextToSpeech.LANG_NOT_SUPPORTED
) {
throw Exception("The language $locale is not supported.")
}
}
tts.language = locale
tts.setSpeechRate(speechRate)
}
}
Code language: Kotlin (kotlin)
Step 6: Using TextToSpeechManager
class
If you’ve reached to this point then congratulations, you’ve understood what I’ve tried to explain.
Now, let us implement and try to actually see the output of the source code in action.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// initialize the text to speech manager class
TextToSpeechManager.init(this)
val text = findViewById<EditText>(R.id.text)
val speak = findViewById<Button>(R.id.button)
speak.setOnClickListener {
val toSpeak = text.text.toString()
// set the language to be used for the speech conversion
val language = Locale("en", "US")
// invoke the method with the text and the language
TextToSpeechManager.instance.speak(toSpeak, language)
}
}
}
Code language: Kotlin (kotlin)
The code above is the basic activity class that I have in my application. You might have something similar. The crucial steps are invoking the TextToSpeechManger.init()
before you try to convert any text to speech.
After the initialization you can invoke the TextToSpeechManager.instance.speak()
method from any part in your application.
And that concludes our Android TTS implementation in Android. It is pretty straight forward to implement it in your application. You can create variety of things with TTS.
Keep learning, until next time!
You might also like:
Android Keystore: Store Sensitive Data in Android