Android easy to implement speech recognition and example code _android

Source: Internet
Author: User

Using intent to invoke speech recognition programs

Description

Android mainly through recognizerintent to achieve speech recognition, in fact, the code is relatively simple, but if the speech recognition device can not be found, will throw an exception activitynotfoundexception, so we need to catch this exception. and speech recognition can not be tested on the simulator, because speech recognition is to access the Google Cloud data, so if the mobile phone network is not open, you can not achieve the recognition of sound! Be sure to open the phone network, if the phone does not exist speech recognition function, it is also unable to enable recognition!

Note: You need to install the speech recognition program before you use it. such as "Speech search", the speech recognition technology used by Google,intent can be recognized by the program.

This example is referenced from the Android routine:

Development/samples/apidemos/src/com/example/android/apis/app/voicerecognition.java

Core code and description

Package com.example.test;
Import android.app.Activity;
Import android.content.Intent;
Import Android.content.pm.PackageManager;
Import Android.os.Bundle;
Import android.speech.RecognizerIntent;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;
Import java.util.ArrayList;

Import java.util.List; public class Mainactivity extends activity implements Onclicklistener {private static final int voice_recognition_reques

 T_code = 1234;
  @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.activity_main); Button btn = (button) Findviewbyid (R.ID.BTN);
  Recognition button Packagemanager PM = Getpackagemanager (); List activities = pm.queryintentactivities (new Intent (Recognizerintent.action_recognize_speech), 0); Local Recognition Program//New Intent (Recognizerintent.action_web_search), 0);
   Network Recognizer * * This is not used to catch exceptions, but to detect if there is a speech recognition program. * can also be in StartrecognizThe Eractivity () method captures the Activitynotfoundexception exception/if (Activities.size ()!= 0) {Btn.setonclicklistener (this);
   else {//If the speech recognition program is not detected to be installed in the machine, the btn.setenabled (false) will be measured with an ammonium twist;
  Btn.settext ("No speech recognition device detected");
  } public void OnClick (View v) {if (V.getid () = = r.id.btn) {startrecognizeractivity (); }///start to identify private void startrecognizeractivity () {///Intent to pass speech recognition mode, turn on speech Intent Intent = new Intent (Recogniz
  Erintent.action_recognize_speech); Language mode and Free mode speech recognition Intent.putextra (Recognizerintent.extra_language_model, Recognizerintent.language_model_free_
  FORM);
  Prompt speech starts Intent.putextra (Recognizerintent.extra_prompt, "Start speech");
  Start speech recognition Startactivityforresult (intent, Voice_recognition_request_code); Pull up the identification interface} @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {//callback gets the number from Google According to if (Requestcode = = Voice_recognition_request_code && ResultCode = = RESULT_OK) {//Get speech characters ArrayList <string> results = data. Getstringarraylistextra (Recognizerintent.extra_results);
   String resultstring = "";
   for (int i = 0; i < results.size (); i++) {resultstring + = Results.get (i);
  } toast.maketext (this, resultstring, Toast.length_short). Show ();
 The callback after speech recognition, will identify the string to toast display Super.onactivityresult (Requestcode, ResultCode, data);

 }
}

The main principle is to send voice to the Google Cloud, and then the cloud processing, matching the corresponding data, sent to the client.

Finally, don't forget to add network access to the manifest:

<uses-permission android:name= "Android.permission.INTERNET"/>

Post-run Effects:

The above is the realization of Android speech recognition data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

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.