Android Speech Recognition Technology

Source: Internet
Author: User

I accidentally saw it online. So I wrote an example based on the online example. It is easy to implement speech recognition. Later TTS was not done.

Directly paste code

View Code

/*** Speech Recognition *** @ author sy **/public class SpeakToActivity extends Activity {/** Called when the activity is first created. */private Button mSpeakButton; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); mSpeakButton = (Button) findViewById (R. id. openSpeak); mSpeakButton. setOnClickListener (new ButtonOnClick ();}/*** button click event ** @ author sy **/class ButtonOnClick implements OnClickListener {@ Override public void onClick (View v) {try {// TODO Auto-generated method stub Intent intent = new Intent (RecognizerIntent. ACTION_RECOGNIZE_SPEECH); // use Intent to start Speech Recognition /// Speech Recognition intent in both language and free modes. putExtra (RecognizerIntent. EXTRA_LANGUAGE_MODEL, RecognizerIntent. LANGUAGE_MODEL_WEB_SEARCH); intent. putExtra (RecognizerIntent. EXTRA_PROMPT, "Start voice"); // only prompts the voice to start startActivityForResult (intent, 10); // The requestCode} catch (Exception e) {e in the onActivityResult method. printStackTrace (); // If the Voice device cannot be found, the Toast message is displayed. makeText (SpeakToActivity. this, "cannot find the Voice device", Toast. LENGTH_LONG ). show () ;}}/*** activity callback function, corresponding to startActivityForResult ** @ author sy */@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stub super. onActivityResult (requestCode, resultCode, data); if (requestCode = 10 & resultCode = RESULT_ OK) {ArrayList <String> results = data. getStringArrayListExtra (RecognizerIntent. EXTRA_RESULTS); // obtain the result set String resultString = ""; // if you use this annotation code, google returns five words that are similar to your pronunciation // for (int I = 0; I <results. size (); I ++) // {// resultString + = results. get (I); //} resultString + = results. get (0); // I select the first Toast by default. makeText (SpeakToActivity. this, resultString, Toast. LENGTH_LONG ). show ();}}}

 

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.