SpeechRecognizer is located in the android.speech package of the source code (API 29--Android 9.0). Here is a simple call to Demo about the native class. So that you are not familiar with the feasibility of this kind of rapid verification technology.
Tips: 1.SpeechRecognizer will connect to the server. You need to ensure that your device can connect to Google.
2. Note to log off the Recognizer when it is not needed, or there will be an activity leak error.
You need to apply for permission dynamically after 「 3.Android 6.0. It can't be used only when declared in Android manifest.xml.
Android Docs location of Android SpeechRecognizer:
https://developer.android.com/reference/android/speech/SpeechRecognizer.html
Source location of Android speech recognizer: (API 29--Android 9.0)
/frameworks/base/core/java/android/speech/SpeechRecognizer.java
You can view the source code in the Android XRef website:
http://androidxref.com/9.0.0_r3/xref/frameworks/base/core/java/android/speech/SpeechRecognizer.java
Demo needs to add its own code at two points:
① there is a Listener in it. We write the operation logic of the identified result here:
② use Intent to specify the attribute of Recognize, modify the converted language, etc.
Put the Recognizer in an empty Activity, code of MainActivity: (pay attention to the package, change the package)
package com.huanyu.speechrecognizedemo; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import android.Manifest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.speech.RecognitionListener; import android.speech.RecognizerIntent; import android.speech.SpeechRecognizer; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import java.util.ArrayList; import java.util.Locale; public class MainActivity extends AppCompatActivity { private String TAG = "SpeechRecognizer"; private SpeechRecognizer speechRecognizer; final int MY_PERMISSIONS_REQUEST_RECORD_AUDIO = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); speechRecognizer = SpeechRecognizer.createSpeechRecognizer(MainActivity.this); speechRecognizer.setRecognitionListener(new SampleRecognitionListener()); Log.d(TAG, "Start Recognize"); Log.d(TAG, "Recognize is available:" + speechRecognizer.isRecognitionAvailable(MainActivity.this)); Button button1 = findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { requestPermission(); Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName()); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.CHINESE.toString()); speechRecognizer.startListening(intent); } }); Button button2 = findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //Speech Recognizer will automatically detect the end of speech, but with this method, you can manually stop the Recognizer speechRecognizer.stopListening(); } }); } @Override protected void onStop() { super.onStop(); speechRecognizer.cancel(); speechRecognizer.destroy(); } class SampleRecognitionListener implements RecognitionListener { @Override public void onReadyForSpeech(Bundle bundle) { Log.d(TAG, "onReadyForSpeech Start"); Log.d(TAG, "onReadyForSpeech End"); } @Override public void onBeginningOfSpeech() { Log.d(TAG, "onBeginningOfSpeech Start"); Log.d(TAG, "onBeginningOfSpeech End"); } @Override public void onRmsChanged(float v) { Log.d(TAG, "onRmsChanged Start"); Log.d(TAG, "onRmsChanged End"); } @Override public void onBufferReceived(byte[] bytes) { Log.d(TAG, "onBufferReceived Start"); Log.d(TAG, "onBufferReceived End"); } @Override public void onEndOfSpeech() { Log.d(TAG, "onEndOfSpeech Start"); Log.d(TAG, "onEndOfSpeech End"); } @Override public void onError(int error) { Log.d(TAG, "onError Start"); switch (error) { case SpeechRecognizer.ERROR_NETWORK_TIMEOUT: resetText("Network link timeout"); break; case SpeechRecognizer.ERROR_NETWORK: resetText("Network error or no permission"); break; case SpeechRecognizer.ERROR_AUDIO: resetText("Audio error"); break; case SpeechRecognizer.ERROR_CLIENT: resetText("link error"); break; case SpeechRecognizer.ERROR_SERVER: resetText("Server error"); break; case SpeechRecognizer.ERROR_SPEECH_TIMEOUT: resetText("Nothing"); break; case SpeechRecognizer.ERROR_NO_MATCH: resetText("No matching results"); break; case SpeechRecognizer.ERROR_RECOGNIZER_BUSY: resetText("RecognitionService Already started,Please wait a moment."); break; case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS: resetText("Please give APP Jurisdiction,Another please. Android6.0 Above) confirm dynamic application authority"); break; default: break; } Log.d(TAG, "onError End"); } @Override public void onResults(Bundle results) { Log.d(TAG, "onResults Start"); String key = SpeechRecognizer.RESULTS_RECOGNITION; ArrayList mResult = results.getStringArrayList(key); String[] result = new String[0]; if (mResult != null) { result = new String[mResult.size()]; } if (mResult != null) { mResult.toArray(result); } Log.d(TAG, "Recognize Result:" + result); resetText(result[0]); Log.d(TAG, "onResults End"); } @Override public void onPartialResults(Bundle bundle) { Log.d(TAG, "onPartialResults Start"); Log.d(TAG, "onPartialResults End"); } @Override public void onEvent(int i, Bundle bundle) { Log.d(TAG, "onEvent Start"); Log.d(TAG, "onEvent End"); } } //Set Text for EditText private void resetText(String text) { EditText result = findViewById(R.id.editText); result.setText(text); } //Remember to request permissions dynamically after 6.0, or you will always be prompted for network errors. private void requestPermission() { Log.d(TAG, "requestPermission"); ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, MY_PERMISSIONS_REQUEST_RECORD_AUDIO); } }
The layout file corresponding to Activity. Name (Activity main. XML):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="100dp"></LinearLayout> <TextView android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Google Speech Recognize Test" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> <Button android:text="~Start~" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1"/> <Button android:text="~End~" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp"></LinearLayout> <EditText android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/editText"/> </LinearLayout>
Demo layout effect:
Manifest file Android manifest: (note to add permissions)
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xingfei.speechrecognizedemo"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Detailed source code analysis and other requirements are completed before writing.