Use Texttospeech controls in Android to read aloud text

Source: Internet
Author: User
Tags locale

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.