Simple use of TextToSpeech in Android, androidtextview attribute
Android can also read the input text and use TextToSpeech. However, currently, only five languages are supported: English, French, German, Italian, and Spanish. (Sorry, there is no Chinese). The android system must be android 1.6 (API8) or later:
Public class SpeechTestActivity extends Activity {
/** TextToSpeech object */
Private TextToSpeech mText2Speech;
/** OK button */
Private Button mBtn;
/** Text input box */
Private EditText mEdt;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
FindViewsById ();
InitListeners ();
MBtn = (Button) findViewById (R. id. test_btn );
MEdt = (EditText) findViewById (R. id. test_edt );
MBtn. setEnabled (false );
}
Private void initListeners (){
MText2Speech = new TextToSpeech (this, new OnInitListener (){
@ Override
Public void onInit (int status ){
If (status = TextToSpeech. SUCCESS) {/** if TTS is loaded successfully */
Int result = mText2Speech. setLanguage (Locale. ENGLISH);/** there are Locale. CHINESE, but CHINESE is not supported */
If (result = TextToSpeech. LANG_MISSING_DATA/** indicates that the language data is lost. */
| Result = TextToSpeech. LANG_NOT_SUPPORTED) {/** unsupported language */
Toast. makeText (SpeechTestActivity. this, "I cannot say", Toast. LENGTH_SHORT). show ();
} Else {
MBtn. setEnabled (true );
MText2Speech. speak ("I miss you", TextToSpeech. QUEUE_FLUSH,
Null );
}
}
}
});
MBtn. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
MText2Speech. speak (mEdt. getText (). toString (),
TextToSpeech. QUEUE_FLUSH, null );
}
});
}
Private void findViewsById (){
MBtn = (Button) findViewById (R. id. test_btn );
MEdt = (EditText) findViewById (R. id. test_edt );
}
@ Override
Protected void onDestroy (){
If (mText2Speech! = Null ){
MText2Speech. stop ();
MText2Speech. shutdown ();
}
Super. onDestroy ();
}
}