Code example for TextToSpeech

Methods: isLanguageAvailableisSpeakingspeakstop

0
	 *  
	 * @return true, if locale is available and supported 
	 */ 
	public boolean checkTTSSupportLanguage() { 
		Locale currentLocale = Locale.getDefault();
		return mTts.isLanguageAvailable(currentLocale) == TextToSpeech.LANG_MISSING_DATA
				|| mTts.isLanguageAvailable(currentLocale) == TextToSpeech.LANG_NOT_SUPPORTED ? false
				: true; 
	} 
 
	/** 
	 * Check keyguard screen is showing or in restricted key input mode . 
	 *  
	 * @return true, if in keyguard restricted input mode 
	 */ 
	public boolean checkKeyguardMode() { 
		getApplicationContext(); 
		KeyguardManager kgMgr = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
		return kgMgr.inKeyguardRestrictedInputMode();
	} 
 
	/** 
	 * Interrupts the current utterance if speaking and speak new text 
	 *  
	 * @param textToSpeech the text to speech 
	 */ 
	public void speakText(String textToSpeech) {
		if (mTts != null) {
			if (mTts.isSpeaking()) {
				mTts.stop();
			} 
			if (checkTTSSupportLanguage() && !checkKeyguardMode()) { 
				mTts.speak(textToSpeech, TextToSpeech.QUEUE_FLUSH, null);
			} 
		}else{ 
			startTts(); 
		} 
	} 
Stop searching for code, let great code find you!  Add Codota to your java IDE