Android voice broadcast, background broadcast, voice recognition

Source: Internet
Author: User

Android voice broadcast, background broadcast, voice recognition



This paper introduces the function of voice broadcast and speech recognition using the voice flight speech.

Flying Open Platform: http://www.xfyun.cn/index.php/default/index


Program:


a simple XML layout

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "&G    T <edittext android:id= "@+id/et" android:layout_width= "fill_parent" android:layout_height= "Wrap_cont Ent "android:editable=" true "/> <button android:id=" @+id/bt_recognize "android:layout_width=    "Fill_parent" android:layout_height= "wrap_content" android:gravity= "center" android:text= "recognition"/> <button android:id= "@+id/bt_speek" android:layout_width= "fill_parent" android:layout_height= "Wra P_content "android:gravity=" center "android:text=" Speek "/> <button android:id=" @+id/bt_spee K_BG "android:layout_width=" fill_parent "android:layout_height=" wrap_content "android:gravity=" center "Android:text=" Backstage SpeeK "/> <textview android:textcolor=" @android: Color/white "android:layout_width=" Wrap_content "        android:layout_height= "Wrap_content" android:text= "Voice control switch"/> <togglebutton android:id= "@+id/tb" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"/></linearlayout>


Identify

Case R.id.bt_recognize:                    //This is the Language Recognition section, the most important instantiation of a                    //Recognizerdialog and put you on the official website application AppID fill in, illegal ID can not be identified                    Recognizerdialog Isrdialog = new Recognizerdialog (voice1activity.this, APPID);                    /                     * * Setting engine currently supports five types of "SMS": Normal text transliteration "poi": Place name Search "Vsearch": Hot Word Search                     * "video": Video Music Search "ASR": Command word recognition */                    Isrdialog.setengine ("SMS", NULL, NULL);                    Isrdialog.setlistener (Recolistener);                    Isrdialog.show ();                    Break

Language recognition listener, there are two methods recognizerdialoglistener Recolistener = new Recognizerdialoglistener () {@Override publi c void Onresults (Arraylist<recognizerresult> results, Boolean islast) {//new added a togglebut                Ton TB, first check if TB is pressed, if it is pressed for language control, do not press the word recognition if (tb.ischecked ()) {//Dovoice method is to identify            DoVoice (results);                } else {//the server will return the collection when it is identified, we will only get the most matching item in the text + = Results.get (0). text;            System.out.println (text);        }}//Iterate the result first, then fetch each result and compare it, and if it contains a specific string, then execute the corresponding intent jump.        Note that all intent can do (e-mail, jump to installed applications, dialing, texting, sending MMS, browsing the web, playing multimedia), it can be done.            private void DoVoice (arraylist<recognizerresult> results) {Intent i = new Intent ();                    for (Recognizerresult result:results) {if (Result.text.contains ("weather")) {///weather interface Jump I.setclass (Voice1activity.this, Weather.class);                   StartActivity (i); } else if (Result.text.contains ("News")) {//Press I.setclass (Voice1activity.this,                    News.class);                StartActivity (i);                     } else if (Result.text.contains ("SMS")) {////SMS Interface Jump I.setaction (Intent.action_view);                    I.settype ("vnd.android-dir/mms-sms");                StartActivity (i); } else {//If the user Toast.maketext is prompted with a Toast without a corresponding instruction (voice1activity.this, "unrecognized", Toast.len                Gth_short). Show ();                }}} @Override public void OnEnd (Speecherror error) {if (error = = null) {            After completion, the results are displayed on the EditText et.settext (text); }        }    };


Speek

Case R.id.bt_speek:                    //This is the language synthesis section that also needs to instantiate a synthesizerdialog and input AppID                    synthesizerdialog syn = new Synthesizerdialog (Voice1activity.this, APPID);                    Syn.setlistener (New Synthesizerdialoglistener () {                        @Override public                        void OnEnd (Speecherror arg0) {                        }                    });                    The Speech synthesis                    syn.settext (Et.gettext (). toString (), NULL) is implemented according to the contents of the EditText;                    Syn.show ();                    Break


Backstage Speek

Case R.ID.BT_SPEEK_BG:                    //This is a background reading, instantiating a synthesizerplayer                    synthesizerplayer player = Synthesizerplayer.createsynthesizerplayer (Voice1activity.this, APPID);                    To set up a voice reader, you can set the reading for both sexes, depending on the API documentation and the official forum                    player.setvoicename ("Vivixiaoyan");//Set up a voice broadcast candidate for example: Vivixiaoyan, Vivixiaomei, Vivixiaoqi                    player.playtext (Et.gettext (). toString (), "ent=vivi21,bft=5", null);                    Break



source Download:http://download.csdn.net/download/gao_chun/8775975


Android voice broadcast, background broadcast, voice recognition

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.