English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Use Intent to call the speech recognition program
Description
In Android, speech recognition is mainly realized through RecognizerIntent, and the code is relatively simple. However, if the speech recognition device cannot be found, it will throw an exception ActivityNotFoundException, so we need to catch this exception. Moreover, speech recognition cannot be tested on an emulator because it accesses Google cloud data. Therefore, if the mobile phone network is not turned on, it is impossible to realize the recognition of sound! You must turn on the mobile phone network, and if the mobile phone does not have the speech recognition function, it is also impossible to enable recognition!
Note: You need to install a speech recognition program before using it. Such as 'Voice Search', the speech recognition technology it uses comes from Google, and Intent can recognize this program.
This example is based on the android example:
development/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.java
Core code and description
package com.example.test; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.speech.RecognizerIntent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends Activity implements OnClickListener { private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) findViewById(R.id.btn); // Recognition button PackageManager pm = getPackageManager(); List activities = pm.queryIntentActivities(new Intent( RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); // Local recognition program // new Intent(RecognizerIntent.ACTION_WEB_SEARCH), 0); // Network recognition program /* * No exception handling is used here, but it checks whether there is a voice recognition program. * ActivityNotFoundException exceptions can also be caught in the startRecognizerActivity() method */ if (activities.size() != 0) { btn.setOnClickListener(this); } // If the voice recognition program is not installed on the local machine, the button will be grayed out btn.setEnabled(false); btn.setText("No voice recognition device detected"); } } public void onClick(View v) { if (v.getId() == R.id.btn) { startRecognizerActivity(); } } // start recognition private void startRecognizerActivity() { // Pass the speech recognition mode through Intent to start voice recognition Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); // language model and free-form speech recognition intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); // prompt the start of voice intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Start voice"); // start voice recognition startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); // invoke the recognition interface } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // callback to obtain data obtained from Google if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) // Get the character of the voice ArrayList<String> results = data .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); String resultString = ""; for (int i = 0; i < results.size(); i++) { resultString += results.get(i); } Toast.makeText(this, resultString, Toast.LENGTH_SHORT).show(); } // The callback after voice recognition will display the recognized string in Toast super.onActivityResult(requestCode, resultCode, data); } }
The main principle is to send the voice to the Google cloud, then process it in the cloud, match the corresponding data, and send it to the client.
Don't forget to add network access permissions in the manifest:
<uses-permission android:name="android.permission.INTERNET" />
Effect after running:
This is the summary of the materials for implementing voice recognition in Android. We will continue to supplement relevant materials in the future. Thank you all for your support of this site!
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to notice#w.3Please report any violations by email to codebox.com (replace # with @ in the email address), and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.