Android implements speech recognition code

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:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 The eight-a-a-i-am-i-M-i-M-i-M-102 * 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_RECOG Nition_request_code = 1234;   Private ListView mlist;  /** * Called with the ' activity ' is a-created. * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);  //Inflate we U I 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 An 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 thought it could have heard ArrayList matches = Data.getstringarrayl Istextra (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.

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.