The realization method of speech recognition in Android programming _android

Source: Internet
Author: User
Tags sqlite database

This article describes the implementation of the speech recognition method of Android programming. Share to everyone for your reference, specific as follows:

Speech recognition technology is widely used in mobile phones, the most common way for human communication is voice, while in mobile applications, mostly through the hardware manual input, this is still the main way of interacting with the mobile phone. But with the mobile phone hardware and software functions of the continuous improvement, it can be foreseen in the near future, voice communication will be the main way of human-computer interaction. The Siri voice helper built into the iphone is a good example. What's known is that the speech recognition technology it uses comes from Google. To be aware, Google-pushed Android is naturally embedded in the most core speech recognition technology, it also integrates Google's cloud technology to better achieve human-computer voice interaction.

Android mainly through the recognizerintent to achieve speech recognition, it mainly includes some constants to represent the mode of speech, etc., as shown in the following table:


Constant

Describe

Action_recognize_speech

Turn on voice activity

Action_web_search

Open the network voice mode, the result will be a web search display

Extra_language

Set Language Library

Extra_language_model

Speech recognition mode

Extra_max_results

The maximum result returned

Extra_prompt

Prompts the user to start the speech

EXTRA _results

Returns a string to a ArrayList

Language_model-free_form

Free language in one language mode

Language_model-web_search

Using the language model to search on the Web

Result_audio_error

The audio encountered an error while returning the result

Result_client_error

The client encountered an error while returning the result

Result_network_error

The network encountered an error while returning results

Result_no_match

No speech errors detected

Result_server_error

The server encountered an error while returning results


Here we only need to pass intent to send an action as well as some attributes, and then start the speech through Startactivityforresult, the code is as follows:

Intent intent= New Intent (Recognizerintent.action_recognize_speech);
    Intent.putextra (recognizerintent.extra_language_model,recognizerintent.language_model_free_form);
    Intent.putextra (Recognizerintent.extra_prompt, "Start speech");

Of course, if the setting is not found, it throws activitynotfoundexception, so we need to catch this exception. In the following example, we implement a simple speech recognition program, when we click the "Click to use Speech Recognition" button, start the voice, and then in the Onactivityresult method to obtain results and display, if the current phone is not connected to the Internet will show connectivity problems, The results of the operation are as follows:

After clicking the button:

If there is no networking there will be a connection error:

After the word "computer" is spoken to the microphone:

The code for the implementation is as follows:

public class Recognizerintent_testactivity extends activity {private static final int voice_recognition_request_code =
  1;
  Private ListView mlist;
  Button Mbutton;
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    Mbutton = (Button) Findviewbyid (R.id.button1);
    Mbutton.setonclicklistener (New Myrecognizerintentlistener ());
  Mlist = (ListView) Findviewbyid (R.ID.LISTVIEW1);
        public class Myrecognizerintentlistener implements Onclicklistener {public void OnClick (View v) {try { Use Intent to pass the mode of speech recognition and turn on the speech mode Intent Intent = new Intent (RECOGNIZERINTENT.ACTION_RECOGNIZE_SPEEC
        H); Language mode and free form of speech recognition Intent.putextra (Recognizerintent.extra_language_model, Recognizerintent.language_mod
        El_free_form);
        Prompt language start Intent.putextra (recognizerintent.extra_prompt, "Please start Speech"); Start speech recognition StartactivityforrEsult (Intent, Voice_recognition_request_code);
            catch (Activitynotfoundexception e) {toast.maketext (recognizerintent_testactivity.this, "Voice device not found",
      Toast.length_long). Show ();
    }}//speech at the end of the callback function @Override protected void onactivityresult (int requestcode, int resultcode, Intent data) {
      if (Requestcode = = Voice_recognition_request_code && ResultCode = = RESULT_OK) {//Get speech characters
      arraylist<string> results = data. Getstringarraylistextra (Recognizerintent.extra_results); Set the update for the View Mlist.setadapter new Arrayadapter<string> (this, Android.
      R.layout.simple_list_item_1, results));
      String resultsstring = "";
      for (int i = 0; i < results.size (); i++) {resultsstring + = Results.get (i);
    } toast.maketext (this, resultsstring, Toast.length_long). Show ();
  } super.onactivityresult (Requestcode, ResultCode, data);

 }
}

For more information on Android-related content readers can view the site: "Android Multimedia how-to Summary (audio, video, recording, etc.)", "Android Development introduction and Advanced Course", "Android view summary of the tips", " The activity Operation skill summary of Android programming, "Android operation SQLite database Skill Summary", "Android operation JSON format Data technique summary", "Android database Operation skill Summary", "Android File Operation skill Summary "," Android programming development of SD card operation method Summary "," Android Resource Operation skills Summary "and" Android Control usage Summary "

I hope this article will help you with the Android program.

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.