Android speech recognition based on _android Voice SDK

Source: Internet
Author: User
Tags documentation function prototype stub

First, preparatory work

1, you need Android phone application development Basics

2, hkust voice Recognition SDK Android version

3, HKUST voice recognition development API document

4, Android Phone

For the Hkust Flying SDK and API documentation, please go to hkust Voice website to download: http://www.xfyun.cn/

Of course there are multiple versions of the SDK and API available, according to your needs to download, second, download need to fill in information to apply for registration, application or can be obtained AppID

The following figure, apply for a appid, it's OK.

Second, speech recognition process

1. Create a recognition control

Function prototypes

Public Recognizerdialog (Context context,string params)

Where the context represents the current contextual environment, this can be passed

Params have parameters as detailed in the API documentation

2, with AppID login to the Hkust Flying server (automatic connection, need networking)

Mainly used in the Speechuser (Com.iflytek.speech) class under the GetUser (). Login () function

where GetUser () means to obtain user objects, can achieve user login, logoff and other operations

Login function prototype

Public Boolean login (context context,string usr,string pwd,string

Parameters as described in the API documentation

3. Read language recognition grammar

See API documentation for details

4, set the identification parameters and identify the listener

Setting parameters through the Setengine () method under Recognizerdialog

Function prototypes

public void Setengine (String engine,string params,string Grammar)

For detailed parameters please refer to the API documentation

5. Recognition result Callback

The Recognizerdialoglistener interface needs to be implemented, and there are two methods that need to be rewritten, respectively

1) public void Onresults (arraylist<recognizerresult> results,boolean islast)

Where result is a collection of Recognizerresult objects, Recognizerresult attributes have

String Text recognition text

INT confidence recognition credibility

2) public void OnEnd (speecherror error)

6, Recognition results processing (self-processing)

Handle the text yourself.

Third, the detailed development process

1. New Android Project

Just like the normal Android program, we need to join the Hkust Language SDK package, which mainly includes

Msc.jar and libmsc.so Dynamic Library files, project Lib screenshot

2, layout

This is only a simple layout, set only one button as the language recognition button and a text component for display recognition results, the layout file is as follows:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
  xmlns:tools= "http:// Schemas.android.com/tools "
  android:layout_width=" fill_parent "
  android:layout_height=" Fill_parent
  " android:orientation= "vertical"
  tools:context= "${relativepackage}.${activityclass}" >

  <edittext
    android:id= "@+id/edittext"
    android:layout_width= "fill_parent"
    android:layout_height= "300DP"
    android:gravity= "Top"
    android:inputtype= "Textmultiline" >
    
    <requestfocus/>
   </ edittext>
   <button
     android:id= "@+id/button_start"
     android:layout_width= "Wrap_content"
     android:layout_height= "wrap_content"
     android:text= "click Start speak"/>
     
</LinearLayout>

3, Main.java code is as follows:

public class Main extends activity {//Component private button button = null;
  Private TextView result = NULL;

  Private Toast mtoast = null;
  Speech recognition Private final String app_id = "Change here to own app_id";

  Private Recognizerdialog recognizerdialog = null;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

    Super.setcontentview (R.layout.main);
    Button = (button) Super.findviewbyid (R.id.button_start);

    result = (TextView) Super.findviewbyid (R.id.edittext);
    Initialization identification Mtoast = Toast.maketext (This, "", Toast.length_short);
    Mtoast.setmargin (0f, 0.2f);
    Recognizerdialog = new Recognizerdialog (This, "appid=" + app_id);

    Speechuser.getuser (). Login (this, NULL, NULL, "appid=" + app_id, Loginlistener);

  This.button.setOnClickListener (New Btn ()); Private class Btn implements Onclicklistener {@Override public void OnClick (View v) {//mainactivity . This.voice.setImageResource (R.drawable.voIcelight);
      Recognizerdialog.setlistener (Mrecolistener);
      Recognizerdialog.setengine (NULL, "GRAMMAR_TYPE=ABNF", Grammartext);
    Recognizerdialog.show (); }//Speech recognition User Login listener private Speechlistener Loginlistener = new Speechlistener () {@Override public void OnD ATA (byte[] arg0) {} @Override public void onEvent (int arg0, Bundle arg1) {} @Override public V OID OnEnd (Speecherror arg0) {//TODO auto-generated Method stub if (arg0!= null) {Mtoast.settext ("log
        Record Failure ");
      Mtoast.show ();
        else {mtoast.settext ("login succeeded");
      Mtoast.show ();

  }
    }
  }; Recognition result callback private Recognizerdialoglistener Mrecolistener = new Recognizerdialoglistener () {@Override public vo
    ID onend (speecherror error) {//mainactivity.this.voice.setimageresource (r.drawable.voice); @Override public void Onresults (arraylist<recognizerresult> results, Boolean islast) {//TODO auto-generated method Stub String Text = "";
      Text = results.get (0). text;
      Mtoast.settext ("The recognition result is:" + text);
      Mtoast.show ();
    Result.settext ("The recognition result is:" + text);

}

  };

 }

4, the required permissions:

  <uses-permission android:name= "Android.permission.INTERNET"/>
  <uses-permission android:name= " Android.permission.RECORD_AUDIO "/>
  <uses-permission android:name=" android.permission.ACCESS_NETWORK_ State "/>
  <uses-permission android:name=" Android.permission.ACCESS_WIFI_STATE "/>
  < Uses-permission android:name= "Android.permission.CHANGE_NETWORK_STATE"/>
  <uses-permission android:name = "Android.permission.READ_PHONE_STATE"/>

5, the result screenshot:


The above is the entire content of this article, I hope that the small partners can enjoy.

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.