Code example for TextToSpeech

Methods: isLanguageAvailablesetLanguagesetOnUtteranceCompletedListenersetSpeechRatesleep

0
 
	@SuppressWarnings("deprecation") 
	@Override 
	public void onInit(int arg0)
	{ 
		mTts.setOnUtteranceCompletedListener(this);
 
		Locale l = Locale.getDefault();
		int result = mTts.isLanguageAvailable(l);
		if (result == TextToSpeech.LANG_AVAILABLE || result == TextToSpeech.LANG_COUNTRY_AVAILABLE
				|| result == TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE)
		{ 
 
			mTts.setLanguage(l);
		} else 
		{ 
			Toast.makeText(context, "Lang not available:" + result, Toast.LENGTH_LONG).show();
		} 
 
		mTts.setSpeechRate(0.85f);
		playNext(); 
	} 
 
	@Override 
	public void onDestroy() 
	{ 
		super.onDestroy(); 
 
		if (mTts != null)
		{ 
			mTts.shutdown();
		} 
	} 
 
	@Override 
	public void onUtteranceCompleted(String arg0)
	{ 
		playNext(); 
	} 
 
	private void playNext() 
	{ 
		Message m = messages.poll();
		if (m == null)
		{ 
			try 
			{ 
				Thread.sleep(1000);
			} catch (InterruptedException e)
			{ 
				Log.e(TAG, "SpeechService:onUtteranceCompleted " + e);
				e.printStackTrace();
			} 
			m = messages.poll();
		} 
 
		if (m != null)
		{ 
			m.play(mTts);
		} else 
		{ 
			stopSelf();
		} 
	}