Monday, September 18, 2017

Android text to speech tutorial

Welcome to this post. In this post, we are going to discuss android text to speech.
Before the start, check the official documentation of Android text to speech.

In my case, I use this option with a fab (Floating action button). When the user clicks on this fab button this text to speech is triggered and user heard the speech.
Let's start,
Implement TextToSpeech.OnIntListener on your activity class.
public class DetailsActivity extends AppCompatActivity implements
        TextToSpeech.OnInitListener {
}

Create an instance of TextToSpeech Class
private TextToSpeech toSpeech;

override onIint() methods and follow below steps:
 1. First, we need to check if Text to speech engine is installed or not
if (status != TextToSpeech.ERROR) {
}
2. If the status is not erroring then we set a default language, we set here the US as default.
int result = toSpeech.setLanguage(Locale.US);
3. now check language data is missing or not supported or not installed then we start for install this text to speech engine.
if (result == TextToSpeech.LANG_MISSING_DATA ||
    result == TextToSpeech.LANG_NOT_SUPPORTED ||
    result == TextToSpeech.ERROR_NOT_INSTALLED_YET) {

      Intent installIntent = new Intent();
      installIntent.setAction(
              TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
      startActivity(installIntent);

}
if you missing something check the full code of onInit() method
@Override
    public void onInit(int status) {

        if (status != TextToSpeech.ERROR) {

            int result = toSpeech.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA ||
                    result == TextToSpeech.LANG_NOT_SUPPORTED ||
                    result == TextToSpeech.ERROR_NOT_INSTALLED_YET) {

                Intent installIntent = new Intent();
                installIntent.setAction(
                        TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);

            }

        }
    }
Create a method and add a single parameter, word as String. In my case, I named it as setTTS() and parameter is word as String,
setTTS(String selectedWord)
private void setTTS(String selectedWord) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            toSpeech.speak(selectedWord, TextToSpeech.QUEUE_FLUSH, null, null);

        } else {
            toSpeech.speak(selectedWord, TextToSpeech.QUEUE_FLUSH, null);
        }

    }
Note: below methods is deprecated. So check if the device is lollipop and higher then use utterance as an extra parameter.

One more thing to go.
We need to shut down this TextToSpeech engine when the activity is destroyed. but before we need to check textToSpeech is null or not to avoid any error. If it is not null the call stop and shutdown method. see the code
@Override
protected void onDestroy() {
   super.onDestroy();
   if (toSpeech != null) {
       toSpeech.stop();
       toSpeech.shutdown();
    }
}

That's it 
you finished writing coding. Now call setTTS() method from anywhere in your project. You can use it as Button onClick or TextView onClick. Just pass a string parameter and call the method. As I told you, at the beginning of this post I am using this option in a fab button. you can that code by the following link. But don't be confused. This is the full activity code, so there is a lot of things with TextToSpeech. Click GitHub link.

Now you can use this code. Hope you understand this TextToSpeech logic. and you will be you use this option in your next or existing project.

Thanks for reading this post.
Happy coding.

About Author:

I am Shudipto Trafder,
Since 2015, I write android code. I love to implement new ideas. I use java and also kotlin. Now I am learning Flutter. I love to learn new things and also love to share. And that is the main reason to run this blog.


Let's Get Connected: Facebook Twitter Linkedin Github

No comments :