English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Today, I found an example of voice recognition on the Internet and think it's quite fun, so I post the code here to share with everyone:
In Android, voice recognition is mainly realized through RecognizerIntent. The code is relatively simple, but if the settings cannot be found, it will throw an ActivityNotFoundException exception. Therefore, we need to catch this exception. Moreover, voice recognition cannot be tested on emulators because it accesses Google Cloud data. Therefore, if the mobile network is not turned on, it is impossible to realize voice recognition! You must turn on the mobile network. If the mobile phone does not have a voice recognition function, it is also impossible to enable recognition!
The following is the code in RecognizerIntentActivity:
public class RecognizerIntentActivity extends Activity { private Button btnReconizer; private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.reconizer); btnReconizer = (Button) this.findViewById(R.id.btnRecognizer); btnReconizer.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub try{ //Pass the speech recognition mode through Intent to start speech Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); //Speech recognition with language model and free-form mode intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); //Prompt to start speech intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Start speech"); //Start speech recognition startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); Toast.makeText(getApplicationContext(), "Cannot find speech device", 1).show(); } } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub //Callback to get data obtained from Google if(requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK){ //Acquire the character of speech 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, 1).show(); } super.onActivityResult(requestCode, resultCode, data); } }
Its main principle is to send the voice to the Google cloud, then the cloud processes it, matches the corresponding data, and sends it to the client.
Finally, don't forget to add network access permission in the manifest:
<uses-permission android:name="android.permission.INTERNET" />
After running the effect:
Click the Start Speech button and start speaking (make sure the phone's network is turned on):
I am waiting for cloud data, because this is2G's card, waited for a long time and still could not load, try to use the company's wifi back to the company, if the cloud data is obtained, it will be printed out through the Toast method.
This is the summary of the materials for Android speech recognition technology. We will continue to supplement relevant materials and thank everyone for their support to this site!
Declaration: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not undergo人工 editing, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting. Provide relevant evidence, and once verified, this site will immediately delete the infringing content.)