Speech recognition on Android, a simple example of Google-based speech recognition.

Source: Internet
Author: User

Speech recognition is convenient and easy to use on Android.

However, there is a premise that Google's voice search tool must be installed on Android machines in advance.


Speech recognition technology is added in Android sdk1.5 (recognizerintent). Here we will analyze the built-in API example, which is actually completed through an intent action. There are two main modes:

Action_recognize_speech: Speech Recognition. In this mode, we can capture the text column after speech processing.

Action_web_search: Network Search

This example uses the action_recognize_speech mode. We need to implement the onactivityresult method, which is the callback function after the speech recognition is complete.


The following is the core code:


Package com. Ifeisu. Test. Voice;

Import the Java. Util. ArrayList;
Import the Java. Util. List;
The import android. App. The Activity;
The import android. Content. Intent;
The import android. Content. PM. PackageManager;
The import android. Content. PM. ResolveInfo;
The import android. OS. Bundle;
The import android. Researched. RecognizerIntent;
The import android. View. The view;
The import android. View. The view. An OnClickListener;
The import android. Widget. ArrayAdapter;
The import android. Widget. The Button;
The import android. Widget. ListView.
/ * * *
* speech recognition demo, before using this demo, you need to confirm that Google's voice search tool is installed on your phone or emulator
* voice_search_2.1.4.apk or later
* @ the author at guoxinzz@163.com
* @ website http://www.ifeisu.com
*
* /
Public class VoiceRecognition extends the Activity implements an OnClickListener {

Private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;

Private ListView mList;

/ * *
*
* Called with the activity is first created.
* /

@ Override
Public void onCreate (Bundle savedInstanceState)

{

Super. OnCreate (savedInstanceState);

The setContentView (R.l ayout. Main);

Button speakButton = (Button) findViewById(r.id.tn_speak);

MList = (ListView) the findViewById (R.i d.l ist);

// Check to see if a recognition activity is present

PackageManager PM = getPackageManager ();

List<resolveinfo> activities = pm.queryintentactivities (new Intent(</resolveinfo>
RecognizerIntent. ACTION_RECOGNIZE_SPEECH), 0);

If (activities. The size ()! = 0)

{

SpeakButton. SetOnClickListener (this);

}

The else

{

SpeakButton. SetEnabled (false);

SpeakButton. SetText (" Recognizer not present ");

}

}

Public void onClick (View v)

{

If (v.g etId () = = R.i db tn_speak)

{

StartVoiceRecognitionActivity ();

}

}

Private void startVoiceRecognitionActivity ()

{

// delivery of speech recognition patterns via intents

Intent Intent = new Intent(recognizerint.action_recognize_speech);
// language patterns and free-form speech recognition

Intent. PutExtra (RecognizerIntent EXTRA_LANGUAGE_MODEL,
RecognizerIntent. LANGUAGE_MODEL_FREE_FORM);

// prompt voice to begin

Intent. PutExtra (RecognizerIntent EXTRA_PROMPT,
"Please name the website you want to open.");

// start performing our Intent, voice recognition

StartActivityForResult (intent, VOICE_RECOGNITION_REQUEST_CODE);

}

// the callback function onActivityResult when the voice ends

@ Override
Protected void onActivityResult(int requestCode, int resultCode, Intent data)

{

If (requestCode = = VOICE_RECOGNITION_REQUEST_CODE
&& the resultCode = = RESULT_OK)

{

// gets the character of speech

ArrayList < String > matches = data
GetStringArrayListExtra (RecognizerIntent. EXTRA_RESULTS);

MList. SetAdapter (new ArrayAdapter < String > (this,
Android. R.l ayout simple_list_item_1, matches));

}

Super. OnActivityResult (requestCode, the resultCode, data);

}

} 


1. If the mobile phone does not support voice search, you need to download Google's Voice Search toolkit voice_search_2.1.4.apk, which can be downloaded from hiapk.com or other app stores.

2. Download the source code: demo.voice_recognition.rar. Because csdn does not allow attachments to be uploaded, right-click the connection and select "connect to". Download and change the name to "demo.voice_recognition.rar.



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.