android – TextToSpeech with API 21-ThrowExceptions

Exception or error:

Can someone help me using TTS with API 21.
All examples available are deprecated with version 21

Here’s my code giving error on last line:

Calendar cal = Calendar.getInstance();
                    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                    String text = sdf.toString();
                    btn.setText("Ouvir as Horas");

                    TextToSpeech tts = new TextToSpeech(NightClock.this,(TextToSpeech.OnInitListener) NightClock.this);
                    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);

In Android developers it says that this method is deprecated and replaced by this:

speak(String text, int queueMode, HashMap params)
This method was deprecated in API level 21. As of API level 21, replaced by speak(CharSequence, int, Bundle, String).

Can someone help to code my app.

How to solve:

I searched various site. Finally, I think I could get the answer for your Question…

Instead calling tts.speak() directly, put the following if-else statement.

} else {

Then declare ttsGreater21() and ttsUnder20() as follows.

private void ttsUnder20(String text) {
    HashMap<String, String> map = new HashMap<>();
    map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId");
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, map);

private void ttsGreater21(String text) {
    String utteranceId=this.hashCode() + "";
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, utteranceId);

I confirmed above code with Genymotion VM Android 5.0 and Android 4.4.4.



Try this.


So I guess this is the trick:

    tts.speak("12 e8", TextToSpeech.QUEUE_FLUSH, null, null);
else {
    tts.speak("12 e8", TextToSpeech.QUEUE_FLUSH, null);

I just need to test this on emulator.

By the way, @Aditya since you’ve been so helpful I’ve been stuck in the same project where it should speak that TextToSpeech and turns on the screen but I haven’t manage to turn the screen on. I have tried to use wakelocks and flags from all the examples I’ve found 🙂 This is done trough the proximity sensor that I managed to work. It says the text but doesn’t show the screen. Can you help me on this?

Well practice is the key of success. All the suggested answer by me are perfectly working in my eclipse IDE. Solution of your screen lock is below

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
..screen will stay on during this section..


(1) My activity implements TextToSpeech.OnInitListener

(2) I play my synthesized speech in the onInit method, but I suppose (not tried it) you could play it anytime after onInit() is called. But this is the key, you have to wait for the TextToSpeech engine to initialize.

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
    public TextToSpeech mTTS;

    protected void onCreate(Bundle savedInstanceState) {
        mTTS = new TextToSpeech(this, this);

    public void onInit(int i) {
        mTTS.speak("Hello, how are you?", TextToSpeech.QUEUE_ADD, null, null);


Try this

tts=new TextToSpeech(getBaseContext(),new TextToSpeech.OnInitListener() 
       public void onInit(int status) 


The wakelock, I managed to make ti work this way:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP |
                                    PowerManager.ON_AFTER_RELEASE, "MyWakelock");


Leave a Reply

Your email address will not be published. Required fields are marked *