Code example for TextToSpeech

Methods: setOnUtteranceProgressListener, speak

0
            myHashParams.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, DONE);
            audioManager.requestAudioFocus(this,
                    AudioManager.STREAM_NOTIFICATION,
                    AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
            myLog("text2speak = " + text2speak);
            tts.speak(text2speak, TextToSpeech.QUEUE_ADD, myHashParams);
        } else if (status == TextToSpeech.ERROR) {
            Toast.makeText(TtsActivity.this,
                    "Error occurred while initializing Text-To-Speech engine", 
                    Toast.LENGTH_LONG).show();
        } 
    } 
 
    @SuppressLint("NewApi") 
    @SuppressWarnings("deprecation") 
    public void setTts() { 
        myLog(""); 
        if (Build.VERSION.SDK_INT >= 15) { 
            myLog("Build.VERSION.SDK_INT >= 15"); 
            tts.setOnUtteranceProgressListener(new UtteranceProgressListener() { 
                @Override 
                public void onDone(String utteranceId) { 
                    onDoneSpeaking(utteranceId); 
                }