First of all, thank the original blogger, the code in this article is from the blogger: (original address)http://flycatdeng.iteye.com/blog/1827245
Read aloud the text does not need any permission, the advantage of this control is not the first permission, and then avoid the network to prevent such as the network login (the message fly is actually good, more intelligent, the control can only read simple text)
Layout file
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context= "Com.lizhanqi.www.androidtexttospeech.MainActivity"> <EditTextAndroid:id= "@+id/edittext"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Please enter the words to be recited" /> <ButtonAndroid:id= "@+id/btn_read"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "read" /></LinearLayout>
Code
PackageCom.lizhanqi.www.androidtexttospeech;ImportAndroid.os.Bundle;ImportAndroid.speech.tts.TextToSpeech;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast;ImportJava.util.Locale; Public classMainactivityextendsAppcompatactivityImplementsView.onclicklistener, Texttospeech.oninitlistener {PrivateButton speechbtn;//Button control starts reading aloud PrivateEditText Speechtxt;//What you need to read PrivateTexttospeech Texttospeech;//TTS Object@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); SPEECHBTN=(Button) Findviewbyid (R.id.btn_read); Speechbtn.setonclicklistener ( This); Speechtxt=(EditText) Findviewbyid (R.id.edittext); Texttospeech=NewTexttospeech ( This, This);//parameter Context,texttospeech.oninitlistener } /*** Used to initialize the Texttospeech engine * status:success or Error 2 values * setlanguage setting language, the Help document is written in 22 kinds of * texttospeech.lang_m Issing_data: Represents data loss for a language. * texttospeech.lang_not_supported: Not supported*/@Override Public voidOnInit (intstatus) { if(Status = =texttospeech.success) {intresult =texttospeech.setlanguage (Locale.china); if(Result = =Texttospeech.lang_missing_data|| result = =texttospeech.lang_not_supported) {Toast.maketext ( This, "data is missing or not supported", Toast.length_short). Show (); } }} @Override Public voidOnClick (View v) {if(Texttospeech! =NULL&&!texttospeech.isspeaking ()) {Texttospeech.setpitch (0.0f);//set the tone, the higher the value the more The voice (female), the smaller the value becomes male, 1.0 is the generalTexttospeech.speak (Speechtxt.gettext (). toString (), Texttospeech.queue_flush,NULL); }} @Overrideprotected voidOnStop () {Super. OnStop (); Texttospeech.stop (); //whether or not the TTS is being read aloud is interruptedTexttospeech.shutdown ();//Close, release resources }}
Use Texttospeech controls in Android to read aloud text