3

Somewhat new to Android Studio. Trying to set up a basic TTS app and am getting this the above error on this line:

t1.speak(toSpeak,TextToSpeech.QUEUE_FLUSH,null,null);

As I said, I am somewhat new to Android Studio so I'm not sure what other files people would need to see in order to resolve this, so let me know and I'll provide more info if needed.

BGrossman
  • 77
  • 6

1 Answers1

2

try this.

replace tts with t1, and text with toSpeak and it should work for API above and below 21.

public void speak(String text) {
    if (tts != null) {

        if (tts.isSpeaking()) {
            tts.stop();
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ttsGreater21(text);
        } else {
            ttsUnder20(text);
        }

    }
}

@SuppressWarnings("deprecation")
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);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void ttsGreater21(String text) {
    String utteranceId = this.hashCode() + "";

    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, utteranceId);
}
Angel Koh
  • 10,460
  • 5
  • 53
  • 75