Android realizes speech recognition code _android

Source: Internet
Author: User
Tags google voice recognition

Apple's iphone has Google's technology for voice recognition, and Android, Google's push, will naturally implant its core technology into Android and build it up with Google's cloud technology. So Google Voice recognition's implementation on Android became extremely easy.

Speech recognition, with the help of cloud technology to identify the user's voice input, including voice control technology, the following we will use the API provided by Google to achieve this function. The function point is: To recognize the user input speech through the user voice and print it on the list. The functional interface is as follows:

The user clicks the Speak button to display the interface:

After the user has finished speaking, will submit to the cloud search:

After the cloud search is complete, return the printed data:

* Copyright (C) 2008 the Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
* You could not use this file, except in compliance with the License.  * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by applicable Writing, software * Distributed under the License is distributed on ' as is ' basis, * without Warra
Nties or CONDITIONS of any KIND, either express OR implied.
* The License for the specific language governing permissions and * limitations under the License.
 
* * Package Com.example.android.apis.app;
 
Import COM.EXAMPLE.ANDROID.APIS.R;
Import android.app.Activity;
Import android.content.Intent;
Import Android.content.pm.PackageManager;
Import Android.content.pm.ResolveInfo;
Import Android.os.Bundle;
Import android.speech.RecognizerIntent;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.ArrayAdapter; Import AndrOid.widget.Button;
 
Import Android.widget.ListView;
Import java.util.ArrayList;
 
Import java.util.List;
/** * Sample code that invokes the speech recognition intent API. * * Public class Voicerecognition extends activity implements Onclicklistener {private static final int voice_recognitio
 
N_request_code = 1234;
 
Private ListView mlist;
/** * Called with the ' activity is ' a-created.
 
* * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Inflate our UI from its XML layout description.
 
Setcontentview (r.layout.voice_recognition);
 
Get display items for later interaction Button Speakbutton = (button) Findviewbyid (r.id.btn_speak);
 
Mlist = (ListView) Findviewbyid (r.id.list);
Check to the If a recognition activity is present packagemanager PM = Getpackagemanager ();
List activities = pm.queryintentactivities (new Intent (Recognizerintent.action_recognize_speech), 0); if (activities.size ()!= 0) {Speakbutton.setonclicklistener (this);
 
else {speakbutton.setenabled (false); Speakbutton.settext ("Recognizer not Present");}
/** * Handle The click on the Start Recognition button.
 
* * public void OnClick (View v) {if (V.getid () = R.id.btn_speak) {startvoicerecognitionactivity ();}}
/** * Fire A intent to start of the speech recognition activity.
* * private void startvoicerecognitionactivity () {Intent Intent = new Intent (Recognizerintent.action_recognize_speech);
Intent.putextra (Recognizerintent.extra_language_model, recognizerintent.language_model_free_form);
Intent.putextra (recognizerintent.extra_prompt, "Speech recognition demo");
Startactivityforresult (Intent, Voice_recognition_request_code);
}/** * Handle the results from the recognition activity. * * @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {if (Requestcode = = Voice_reco Gnition_request_code && ResultCode = = RESULT_OK) {//Fill the list view with the strings the recognizer I T could have Heard ArrayList matches = Data.getstringarraylistextra (recognizerintent.extra_results); Mlist.setadapter New Arrayadapter (this, Android.
R.layout.simple_list_item_1, matches));
} super.onactivityresult (Requestcode, ResultCode, data); }
}

The above is the entire contents of this article, I hope you can enjoy.

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.