Speech synthesis of Android audio voice (online audio reading)

Source: Internet
Author: User

How to use online speech synthesis:

First download the relevant SDK, this URL has a variety of versions, I choose Android.

Http://open.voicecloud.cn/index.php/services/voicebase?type=tts&tab_index=1

Note that it is a good idea to create an app first, and a AppID number will be returned after approval, which will be used at development time.

After downloading and extracting

Among them, Doc is related to the document, Libs is to be imported in the speech package, sample is a self-contained demo, you can see.

Here's a simple example of using the online speech synthesis feature:

Create the project first.

To import a voice package, such as:

Next is the layout file

Main.xml:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >    <ButtonAndroid:id= "@+id/btn"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Sound reading" /></LinearLayout>

Then Mainactivity.java:

Note the imported external package:

Import com.iflytek.cloud.SpeechConstant;
Import Com.iflytek.cloud.SpeechError;
Import Com.iflytek.cloud.SpeechSynthesizer;
Import com.iflytek.cloud.SpeechUtility;
Import Com.iflytek.cloud.SynthesizerListener;

 Public classMainactivityextendsActivity {PrivateSpeechSynthesizer MTts; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); Speechutility.createutility ( This, Speechconstant.appid + "53c4c169"); MTts= Speechsynthesizer.createsynthesizer ( This,NULL);        Set_mtts (); Button btn=(Button) Findviewbyid (R.ID.BTN); Btn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {mtts.startspeaking ("July 14, the British Farnborough Air Show officially opened in 2014. The new "Eagle Lion ng" fighter, developed by Swedish Saab, debuted at the airshow. The machine in the Jas-39 "Eagle Lion" on the basis of a comprehensive improvement, the replacement of the latest AESA radar, and equipped with "meteor" medium-range air-missile, IRIS-T Close combat missiles, KEPD-350 remote zone outside the ground missiles and other new generation of airborne weapons, fighting has been unprecedented leap. ", Mttslistener);    }        }); }    Private voidSet_mtts () {//set the pronunciation of peopleMtts.setparameter (Speechconstant.voice_name, "XiaoYan"); //Set speech speedMtts.setparameter (Speechconstant.speed, "20"); //Set ToneMtts.setparameter (Speechconstant.pitch, "50"); //Set Volume 0-100Mtts.setparameter (Speechconstant.volume, "100"); //set the player audio stream typeMtts.setparameter (Speechconstant.stream_type, "3"); //If you do not want to save the synthesized audio, note the following code//Mtts.setparameter (Speechconstant.tts_audio_path,//"./sdcard/iflytek.pcm");    }    PrivateSynthesizerlistener Mttslistener =NewSynthesizerlistener () {//buffering Progress Callback, arg0 to buffer progress, arg1 the position where the buffered audio begins in the text, arg2 the position where the buffered audio ends in the text, ARG3 for additional information@Override Public voidOnbufferprogress (intARG0,intArg1,intarg2, String arg3) {            //TODO auto-generated Method Stub        }        //session End callback interface, error empty when there are no errors@Override Public voidoncompleted (speecherror error) {//TODO auto-generated Method Stub        }        //Start Playback@Override Public voidOnspeakbegin () {//TODO auto-generated Method Stub        }        //Stop playing@Override Public voidonspeakpaused () {//TODO auto-generated Method Stub        }        //The playback Progress callback, arg0 for the playback progress 0-100;arg1 the position where the audio starts in the text, arg2 the position where the audio is to end in the text. @Override Public voidOnspeakprogress (intARG0,intArg1,intarg2) {            //TODO auto-generated Method Stub        }        //Resume Playback Callback interface@Override Public voidonspeakresumed () {//TODO auto-generated Method Stub        }    }; @Overrideprotected voidOnDestroy () {mtts.stopspeaking (); Mtts.destroy ();//release connection when exiting        Super. OnDestroy (); }}

Note: Never forget to add permissions to the manifest file.

<uses-permissionAndroid:name= "Android.permission.RECORD_AUDIO" />    <uses-permissionAndroid:name= "Android.permission.INTERNET" />    <uses-permissionAndroid:name= "Android.permission.ACCESS_NETWORK_STATE" />    <uses-permissionAndroid:name= "Android.permission.ACCESS_WIFI_STATE" />    <uses-permissionAndroid:name= "Android.permission.CHANGE_NETWORK_STATE" />    <uses-permissionAndroid:name= "Android.permission.READ_PHONE_STATE" />

Run the software (remember to connect to the Internet) and click the button to speak aloud.

Demo Download: http://download.csdn.net/detail/af74776/7638817

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.