Android TTS Voice synthesis experience sharing (Iflytek voice +) integration

Source: Internet
Author: User

Application scenario: Foot bath software, technician clock Room arrangement deployment and queue sorting view, voice broadcast reminders. The old program is to use a dual-screen graphics card that runs an unattended desktop program on a Windows system PC. Today's Android Set-top box (WIFI) and mini TV sticks are combined with HDMI and support for large-screen HD LCDs (TVS), which dramatically reduce the cost of this application scenario and make it more practical.

Through a survey, we found that the domestic listed companies Iflytek Voice +SDK Packet word-of-mouth good, good results, integration is also convenient. Voice + support offline application, this is the most important. The integration process goes up:

1, download the demo program. The demo program contains all the resources you need.

2. SDK Integration Key code:

(1) SpeechSynthesizer is a speech synthesis object encapsulated in a jar package she is our heroine.

Speech Synthesis Object Private SpeechSynthesizer mtts;public static String SPEAKER = "SPEAKER";

Instantiate the MTTs code from demo copy. The call method should be a jar in the encapsulated method of outsourcing calls, temporarily do not pay attention to, first focus on how to use them.

private void checkvoiceserviceisinstalled () {//Detect if the speech engine is available if not installed automatically install if (!checkspeechserviceinstall ()) {String assetsapk = "speechservice.apk", if (Processinstall (this, assetsapk)) speechutility.getutility (this). Setappid (" 53c776aa "); return;} Engine initialization speechutility.getutility (this). Setappid ("53c776aa"); mTts = new SpeechSynthesizer (this, mttsinitlistener);}

(2) Check whether the Voice plus engine is installed

/** * Perform Local installation voice + * * @param context * @param assetsapk * @return */private Boolean Processinstall (context context, String assetsapk) {try {thread.sleep (30000);} catch (Interruptedexception e) {e.printstacktrace ();} Local installation mode if (! Apkinstaller.installfromassets (context, assetsapk)) {Toast.maketext (Activity_main.this, "Installing the speech engine failed!", Toast.length_ Short). Show (); return false;} return true;} /** * Detect if Iflytek Voice + engine is installed * * @return */private boolean checkspeechserviceinstall () {String PackageName = "Com.iflytek.speech Cloud "; list<packageinfo> packages = Getpackagemanager (). getinstalledpackages (0); for (int i = 0; i < packages.size (); i+ +) {PackageInfo PackageInfo = Packages.get (i), if (PackageInfo.packageName.equals (PackageName)) {return true;} else { Continue;}} return false;} /** * Speech Synthesis parameter settings * * @param param * @return */private void SetParam () {Mtts.setparameter (speechconstant.engine_type,sp.get String ("Engine_preference", "local")), if (Sp.getstring ("Engine_preference", "local"). Equalsignorecase ("Local ")) {Mtts.setparameter (speechsynthesizer.voice_name,sp.getstring (" Role_cn_preference "," XiaoYan "));} else { Mtts.setparameter (speechsynthesizer.voice_name,sp.getstring ("Role_cn_preference", "XiaoYan")); Mtts.setparameter (speechsynthesizer.speed,sp.getstring ("Speed_preference", ")") Mtts.setparameter ( Speechsynthesizer.pitch,sp.getstring ("Pitch_preference", "a"), Mtts.setparameter (Speechsynthesizer.volume, Sp.getstring ("Volume_preference", "50");}

(3) Initial monitoring, I display the Voice engine available status after the Errorcode.success initialization is successful, otherwise the Red Fork display can not state. Synthetic callback listener, here in order to avoid the asynchronous call, inaudible, using local variables to record the current voice broadcast state Isplaysound. The engine supports asynchronous invocation, and if it does not control it will appear, one sentence is not finished, the other calls in the case of initiating the request, using the queue to control the order of the broadcast.

/** * Initial monitoring. */private Initlistener Mttsinitlistener = new Initlistener () {@Overridepublic void OnInit (ispeechmodule arg0, int code) {I F (Code = = errorcode.success) {btn_voicedemo.setbackgroundresource (r.drawable.voice_enable_style);} else {Btn_ Voicedemo.setbackgroundresource (R.drawable.voice_disable_style);}}};/ * * Synthetic callback listener. */private Synthesizerlistener Mttslistener = new Synthesizerlistener.stub () {@Overridepublic void onbufferprogress (int Progress) throws RemoteException {} @Overridepublic void oncompleted (int code) throws RemoteException {Activity_ Main.this.runOnUiThread (New Runnable () {@Overridepublic void run () {int pos = Voicequeuelist.indexof ( Getcurrentvoiceentity ()); if (pos! =-1) {Currentvoiceentity.setvoiceplaynum (Currentvoiceentity.getvoiceplaynum () + 1) ); Voicequeuelist.remove (POS); if (Currentvoiceentity.getvoiceplaynum () > Integer.parseint (sp.getstring ("Voice_ Play_timers "," 0 "))) {currentvoiceentity.setisvoiceplayed (true); new Execvoicestatuschangebiz (). Execute ( String.valuEOf (Getcurrentvoiceentity (). Getkeyid ()));} Voicequeuelist.add (currentvoiceentity);} Loadvoicetasklist ();}}); Isplaysound = false;} @Overridepublic void Onspeakbegin () throws RemoteException {Isplaysound = true;} @Overridepublic void onspeakpaused () throws remoteexception {} @Overridepublic void onspeakprogress (int progress) throws remoteexception {} @Overridepublic void onspeakresumed () throws remoteexception {}};/** * form destroyed when the Voice service resource is released */@ overrideprotected void OnDestroy () {Super.ondestroy (); mtts.stopspeaking (Mttslistener); Mtts.destory ();}

3, complete. To commemorate the moment:

 

  

Android TTS Voice synthesis experience sharing (Iflytek voice +) integration

Related Article

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.