TTS text to speech Android example

Android development example source code

// import
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
// you have to add implementation
public class Main extends Activity implements TextToSpeech.OnInitListener {
        private int _langTTSavailable = -1; // set up in onInit method
        // declaration
	private TextToSpeech mTts;
 
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
// assigned handle - initialisation
			mTts = new TextToSpeech(this,
			        (OnInitListener) this  // TextToSpeech.OnInitListener
			        );
}
 
   // Implements TextToSpeech.OnInitListener.
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            // Set preferred language to US english.
             _langTTSavailable = mTts.setLanguage(Locale.US); // Locale.FRANCE etc.
            if (_langTTSavailable == TextToSpeech.LANG_MISSING_DATA ||
            	_langTTSavailable == TextToSpeech.LANG_NOT_SUPPORTED) {
             } else if ( _langTTSavailable >= 0) {
			 mTts.speak("Good morning",
            TextToSpeech.QUEUE_FLUSH,  // Drop all pending entries in the playback queue.
            null);
            }
        } else {
            // Initialization failed.
        }
    }
 
    @Override
    public void onDestroy() {
        // TTS shutdown!
        if (mTts != null) {
            mTts.stop();
            mTts.shutdown();
        }
 
        super.onDestroy();
    }
 
}

Get supported language:

private TextToSpeech mTts;
// public void onInit(int status){
int result;
String s;
result = mTts.setLanguage(	Locale.	CANADA	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	CANADA	 not supported<br>"	;}else{s+="	CANADA	 supported<br>";}
result = mTts.setLanguage(	Locale.	CANADA_FRENCH	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	CANADA_FRENCH	 not supported<br>"	;}else{s+="	CANADA_FRENCH	 supported<br>";}
result = mTts.setLanguage(	Locale.	CHINA	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	CHINA	 not supported<br>"	;}else{s+="	CHINA	 supported<br>";}
result = mTts.setLanguage(	Locale.	CHINESE	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	CHINESE	 not supported<br>"	;}else{s+="	CHINESE	 supported<br>";}
result = mTts.setLanguage(	Locale.	ENGLISH	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	ENGLISH	 not supported<br>"	;}else{s+="	ENGLISH	 supported<br>";}
result = mTts.setLanguage(	Locale.	FRANCE	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	FRANCE	 not supported<br>"	;}else{s+="	FRANCE	 supported<br>";}
result = mTts.setLanguage(	Locale.	FRENCH	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	FRENCH	 not supported<br>"	;}else{s+="	FRENCH	 supported<br>";}
result = mTts.setLanguage(	Locale.	GERMAN	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	GERMAN	 not supported<br>"	;}else{s+="	GERMAN	 supported<br>";}
result = mTts.setLanguage(	Locale.	GERMANY	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	GERMANY	 not supported<br>"	;}else{s+="	GERMANY	 supported<br>";}
result = mTts.setLanguage(	Locale.	ITALIAN	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	ITALIAN	 not supported<br>"	;}else{s+="	ITALIAN	 supported<br>";}
result = mTts.setLanguage(	Locale.	ITALY	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	ITALY	 not supported<br>"	;}else{s+="	ITALY	 supported<br>";}
result = mTts.setLanguage(	Locale.	JAPAN	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	JAPAN	 not supported<br>"	;}else{s+="	JAPAN	 supported<br>";}
result = mTts.setLanguage(	Locale.	JAPANESE	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	JAPANESE	 not supported<br>"	;}else{s+="	JAPANESE	 supported<br>";}
result = mTts.setLanguage(	Locale.	KOREA	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	KOREA	 not supported<br>"	;}else{s+="	KOREA	 supported<br>";}
result = mTts.setLanguage(	Locale.	KOREAN	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	KOREAN	 not supported<br>"	;}else{s+="	KOREAN	 supported<br>";}
result = mTts.setLanguage(	Locale.	PRC	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	PRC	 not supported<br>"	;}else{s+="	PRC	 supported<br>";}
result = mTts.setLanguage(	Locale.	ROOT	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	ROOT	 not supported<br>"	;}else{s+="	ROOT	 supported<br>";}
result = mTts.setLanguage(	Locale.	SIMPLIFIED_CHINESE	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	SIMPLIFIED_CHINESE	 not supported<br>"	;}else{s+="	SIMPLIFIED_CHINESE	 supported<br>";}
result = mTts.setLanguage(	Locale.	TAIWAN	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	TAIWAN	 not supported<br>"	;}else{s+="	TAIWAN	 supported<br>";}
result = mTts.setLanguage(	Locale.	TRADITIONAL_CHINESE	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	TRADITIONAL_CHINESE	 not supported<br>"	;}else{s+="	TRADITIONAL_CHINESE	 supported<br>";}
result = mTts.setLanguage(	Locale.	UK	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	UK	 not supported<br>"	;}else{s+="	UK	 supported<br>";}
result = mTts.setLanguage(	Locale.	US	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	US	 not supported<br>"	;}else{s+="	US	 supported<br>";}
This entry was posted in Android Examples Code and tagged , , , , . Bookmark the permalink.


Comments are closed.



10,503 views