Application of Google speech recognition in Android

Source: Internet
Author: User
Tags gettext

Introduction

Due to the end of the six-year project, entered a period of "convalescence" of the vacuum period, so there is time to summarize the experience of the project, learning new posture and skills, to their own skill level is also a promotion. In addition, the individual lazy cancer serious, in order to force themselves to study hard, set up some Falai ( such as not write a blog woman cough cough, I will draw the flag successfully, Hum ), overall, the effect is very good.

The project uses Android and Linux for client development, combined with the actual combat experience of the project, the blog content written in July mainly around the development of Android, the content of the study is scheduled to: Android Google Voice search function inquiry (opening), App app closes unexpectedly after MediaPlayer (estimated two to three chapters)

Why choose android Google Voice search as the first official blog post? The first implementation of the function is relatively simple, the code is very small, can be a follow-up article hot warm-up; second voice search this powerful, especially Google as the IT giant, voice search body sense is very good, fast and accurate recognition. Google's voice search can be widely used with lbs-based Android applications, call the Google API to get search results, and then do what you want based on this result.

To begin with, let's start with a formal introduction to Google's voice search.

Google Voice Search Basics

Speech recognition is the introduction of Google in API Level3, also known as SDK1.5. According to the official SDK, there are 4 modes of voice Retrieval Behavior (ACTION):

Action_get_language_details---> API Level8 Introduction

Action_recognize_speech---> API Level3 Introduction

Action_voice_search_hands_free---> API Level16 Introduction

Action_web_search---> API Level3 Introduction

The approximate functions are:

Action_get_language_details: A broadcast-nature intent used to get meta-data, not commonly used.

Action_recognize_speech: An activity sends what the user says to the speech recognizer, and the results are returned from the Onactivityresult. (Note: The official no longer supports startactivity in the way of intent, changed to Startactivityresult). This section is the core feature.

The new features introduced in ACTION_VOICE_SEARCH_HANDS_FREE:API16 are not commonly used in order to allow users to perform voice searches without using the client, such as in the Safe mode of lock screen. If you want to use this mode, you must include the following in manifest:

1 <  android:name= "Android.speech.action.VOICE_SEARCH_HANDS_FREE"/>

Action_web_search: Implemented through web network retrieval.

The following are the main 3 custom setting items (Option):

Extra_language_model: Language settings for speech recognition

Extra_prompt: Prompt text to display when voice input

Extra_max_results: Maximum value setting for voice search results

Also need to note that in the area without Google services is not able to do Google Voice services, brackets laugh. And the phone does not support voice search, the local need to pre-install a voice pack: voice_search_2.1.4.apk, this version is very old, 2011 has not been updated, the original ecological skin is memorable. If you do not download it in Google-market, you can download it at the following URL:

Http://www.coolapk.com/apk/com.google.android.voicesearch

Implementation of Google Voice search

The implementation approach can be summarized in the following four steps:

1 from a intent event called Recognizerintent

2 constant setting for speech search in Putextra

3 Startactivityresult

4 Onactivityresult wait for search results.

Core code Example:

Add a radiagroup to set up 3 popular voice search modes.

<RadiogroupAndroid:id= "@+id/search_group"android:orientation= "vertical"Android:layout_marginleft= "90DP"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content">        <RadioButtonandroid:checked= "true"Android:text= "Action_recognize_speech"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" />         <RadioButtonAndroid:text= "Action_web_search"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" />         <RadioButtonAndroid:text= "Action_voice_search_hands_free"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" />    </Radiogroup>

In Mainactivity, get Radiogroup selected, the code is as follows

1 PrivateString Getrecognizermode () {2 3          for(inti=0; I<radiobtn.getchildcount (); i++) {4 5RadioButton BTN =(RadioButton) Radiobtn.getchildat (i);6 7             if(btn.ischecked ()) {8 9                 if(Btn.gettext (). Equals (Recognizerintent.action_recognize_speech)) {Ten  One                     returnRecognizerintent.action_recognize_speech; A  -}Else if(Btn.gettext (). Equals (Recognizerintent.action_web_search)) { -  the                     returnRecognizerintent.action_web_search; -  -}Else if(Btn.gettext (). Equals (Recognizerintent.action_voice_search_hands_free)) { -  +                     returnRecognizerintent.action_voice_search_hands_free; -  +                 } A  at                 Else { -  -                     return NULL; -  -                 } -  in             } -  to         } +  -         return NULL; the  *}

Then, after clicking the Voice button, execute startvoicerecognition this method intent operation

1 Private voidstartvoicerecognition (String mode) {2 3         Try {4 5Intent Intent =NewIntent (mode);6 7 Intent.putextra (Recognizerintent.extra_language_model,8 9 recognizerintent.language_model_free_form);Ten  One Intent.putextra (recognizerintent.extra_prompt, r.string.voice_begin); A  -Intent.putextra (Recognizerintent.extra_language, "ZH-HK"); -  the Startactivityforresult (Intent, voice_recognition_request_code); -  -}Catch(Exception e) { -  + e.printstacktrace (); -  +Toast.maketext ( This, "No Google Voice app, plz download.", Toast.length_short). Show (); A  at         } -  -}

In the test, it is found that the content of speech search depends on the system environment of the machine, if the Chinese language system recognizes the Chinese search content, if it is Japanese system, it is the Japanese search content. Recognizerintent.extra_language This attribute does not seem to be a mess.

And finally the Onactivityresult callback results section.

 protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {        Super. Onactivityresult (Requestcode, ResultCode, data); LOG.D ("Lsy", "Onactivityresult--and Requestcode:" + Requestcode + "ResultCode:" +ResultCode); if(Requestcode = = Voice_recognition_request_code && ResultCode = =RESULT_OK) {ArrayList<string > results =Data.getstringarraylistextra (recognizerintent.extra_results); if(Results.size () > 0) {String result= Results.get (0). ToString (). Trim (); if(NULL! = result && result.length () > 0) {Searchresult.settext (result);//get all search results//For (String str:results) {//searchresult.append (str + "/n");//                    }                } Else{Toast.maketext ( This, "Voice Content Fail", Toast.length_short). Show (); }            } Else{LOG.D ("Lsy", "Onactivityresult--and Voice Content Fail"); Toast.maketext ( This, "Voice Content Fail", Toast.length_short). Show (); }         }    }

The results of all searches are saved in a ArrayList list, the first data is the highest match value, and the first data is selected in the code because the most matching values need to be taken for the map search. The comments section can print out all values that satisfy the search criteria.

Summarize

Google's voice search can help us to achieve in the LBS class application of voice localization, its high precision, fast speed, but can not be described the existence of limited the use of this useful function, it is a pity. Follow-up based on Google Voice development content can include: historical voice search records, speech Lenovo function, and independent development of navigation software collaborative work, due to the time rush did not come and realize, interested friends can experiment.

This article on the internet on the Google Voice search articles to summarize and part of the expansion, made a little contribution, very ashamed.

Reference Blog

http://blog.csdn.net/h7870181/article/details/11151773

http://blog.csdn.net/gumanren/article/details/6771265

Application of Google speech recognition in Android

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.