Code example for TextToSpeech

Methods: isLanguageAvailablesetLanguagesetOnUtteranceCompletedListenersetPitchspeak

0
    @Override 
    public void onInit(int status) {
    	Locale locale = new Locale("spa", "MEX");
        if (status == TextToSpeech.SUCCESS) {
            setTtsListener(); 
       	 	tts.setPitch(0.5f);
            switch (tts.isLanguageAvailable(locale))
            { 
                case TextToSpeech.LANG_AVAILABLE:
                case TextToSpeech.LANG_COUNTRY_AVAILABLE:
                case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
                    Log.d(TAG, "SUPPORTED");
                    tts.setLanguage(locale);
                    break; 
                case TextToSpeech.LANG_MISSING_DATA:
                    Log.d(TAG, "MISSING_DATA");
                    // missing data, install it 
                    Intent installIntent = new Intent();
                    installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                    startActivity(installIntent);
                    break; 
                case TextToSpeech.LANG_NOT_SUPPORTED:
                    Log.d(TAG, "NOT SUPPORTED");
                    break; 
            } 
  
            hashAudio.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_MUSIC));
            hashAudio.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "fuppet");
            tts.speak(message, TextToSpeech.QUEUE_ADD, hashAudio);
 
        } 
        else if (status == TextToSpeech.ERROR) {
            Toast.makeText(SpeechActivity.this, 
                    "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
        } 
         
    } 
 
    private void setTtsListener() 
    { 
        int listenerResult = tts.setOnUtteranceCompletedListener(this);
        if (listenerResult != TextToSpeech.SUCCESS)
        { 
            Log.e(TAG, "failed to add utterance completed listener");
        } 
      } 
Stop searching for code, let great code find you!  Add Codota to your java IDE