Code example for TextToSpeech

Methods: playSilence, setOnUtteranceProgressListener, speak

0
            // onInit method (inside setTts()), in the SUCCESS check. 
            setTts(); 
            HashMap<String, String> myHashParams = new HashMap<String, String>();
            myHashParams.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, DONE);
            if (initialize) {
                tts.playSilence(1, TextToSpeech.QUEUE_ADD, myHashParams);
            } else { 
                audioManager.requestAudioFocus(this,
                        AudioManager.STREAM_NOTIFICATION,
                        AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
                myLog("text2speak = " + text2speak);
                tts.speak(text2speak, TextToSpeech.QUEUE_FLUSH, myHashParams);
            } 
        } else { 
            // report back to the caller that TTS is not available 
            Intent intent = new Intent(BROADCAST_ACTION);
            sendBroadcast(intent);
        } 
    } 
 
    @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); 
                }