Code example for TextToSpeech

Methods: isLanguageAvailable, setLanguage, setOnUtteranceCompletedListener, setPitch

0
	    @Override 
	    public void onInit(int status) {
	    	Locale locale = new Locale("en", "EN");
	        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(TAGsp, "SUPPORTED");
	                    tts.setLanguage(locale);
	                    break; 
	                case TextToSpeech.LANG_MISSING_DATA:
	                    Log.d(TAGsp, "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(TAGsp, "NOT SUPPORTED");
	                    break; 
	            } 
	        } 
	        else if (status == TextToSpeech.ERROR) {
	            Log.e(TAGsp, "Error occurred while initializing Text-To-Speech engine");
	        } 
	         
	    } 
	    private void setTtsListener() 
	    { 
	        int listenerResult = tts.setOnUtteranceCompletedListener(this);
	        if (listenerResult != TextToSpeech.SUCCESS)
	        { 
	            Log.e(TAGsp, "failed to add utterance completed listener");
	        } 
	      }