Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
Documents from this version have been archived, and will not continue to be maintained. Please use the latest version.
Before API development, you need to make necessary development preparations, ensure that the Maven repository address of the HMS Core SDK has been configured in your project, and the SDK of this service has been integrated.
1. Call an API to create a speech recognizer.
- // context: app context.
- MLAsrRecognizer mSpeechRecognizer = MLAsrRecognizer.createAsrRecognizer(context);
2. Create a speech recognition result listener callback.
- // Use the callback to implement the MLAsrListener API and methods in the API.
- protected class SpeechRecognitionListener implements MLAsrListener {
- @Override
- public void onStartListening() {
- // The recorder starts to receive speech.
- }
- @Override
- public void onStartingOfSpeech() {
- // The user starts to speak, that is, the speech recognizer detects that the user starts to speak.
- }
- @Override
- public void onVoiceDataReceived(byte[] data, float energy, Bundle bundle) {
- // Return the original PCM stream and audio power to the user.
- }
- @Override
- public void onRecognizingResults(Bundle partialResults) {
- // Receive the recognized text from MLAsrRecognizer.
- }
- @Override
- public void onResults(Bundle results) {
- // Text data of ASR.
- }
- @Override
- public void onError(int error, String errorMessage) {
- // Called when an error occurs in recognition.
- }
- @Override
- public void onState(int state) {
- // Notify the app status change.
- }
- }
3. Bind the new result listener callback to the speech recognizer.
- mSpeechRecognizer.setAsrListener(new SpeechRecognitionListener());
4. Set recognition parameters and callstartRecognizingto start speech recognition.
- // Create an Intent to set parameters.
- Intent mSpeechRecognizerIntent = new Intent(MLAsrConstants.ACTION_HMS_ASR_SPEECH);
- // Use Intent for recognition parameter settings.
- mSpeechRecognizerIntent
- // Set the language that can be recognized to English. If this parameter is not set, English is recognized by default. Example: "zh": Chinese; "en-US": English; "fr-FR": French
- .putExtra(MLAsrConstants.LANGUAGE, "zh")
- // Set to return the recognition result along with the speech. If you ignore the setting, this mode is used by default. Options are as follows:
- // MLAsrConstants.FEATURE_WORDFLUX: Recognizes and returns texts through onRecognizingResults.
- // MLAsrConstants.FEATURE_ALLINONE: After the recognition is complete, texts are returned through onResults.
- .putExtra(MLAsrConstants.FEATURE, MLAsrConstants.FEATURE_WORDFLUX);
- // Start speech recognition.
- mSpeechRecognizer.startRecognizing(mSpeechRecognizerIntent);
5. Release resources after the detection is complete.
- if (mSpeechRecognizer!= null) {
- mSpeechRecognizer.destroy();
- }
Create an Intent to set parameters.
- // Use Intent for recognition settings.
- Intent intent = new Intent(this, MLAsrCaptureActivity.class)
- // Set the language that can be recognized to English. If this parameter is not set, English is recognized by default. Example: "zh": Chinese; "en-US": English; "fr": French
- .putExtra(MLAsrCaptureConstants.LANGUAGE, "en-US")
- // Set whether to display text on the speech pickup UI. MLAsrCaptureConstants.FEATURE_ALLINONE: no; MLAsrCaptureConstants.FEATURE_WORDFLUX: yes.
- .putExtra(MLAsrCaptureConstants.FEATURE, MLAsrCaptureConstants.FEATURE_WORDFLUX);
2. Create a speech pickup UI activity and pass Intent created in step 1 for speech pickup to send the result to the current activity. The speech not longer than 60s can be recognized in real time.
- // 100: request code between the current activity and speech pickup UI activity. You can use this code to obtain the processing result of the speech pickup UI.
- startActivityForResult(intent, 100);
3. Overwrite the onActivityResult method to process the result returned by ASR.
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- String text = "";
- // 100: request code between the current activity and speech pickup UI activity defined in step 2.
- if (requestCode == 100) {
- switch (resultCode) {
- // MLAsrCaptureConstants.ASR_SUCCESS: Recognition is successful.
- case MLAsrCaptureConstants.ASR_SUCCESS:
- if (data != null) {
- Bundle bundle = data.getExtras();
- // Obtain the text information recognized from speech.
- if (bundle != null && bundle.containsKey(MLAsrCaptureConstants.ASR_RESULT)) {
- text = bundle.getString(MLAsrCaptureConstants.ASR_RESULT);
- // Process the recognized text information.
- }
- }
- break;
- // MLAsrCaptureConstants.ASR_FAILURE: Recognition fails.
- case MLAsrCaptureConstants.ASR_FAILURE:
- // Processing logic for recognition failure.
- if(data != null) {
- Bundle bundle = data.getExtras();
- // Check whether a result code is contained.
- if(bundle != null && bundle.containsKey(MLAsrCaptureConstants.ASR_ERROR_CODE)) {
- int errorCode = bundle.getInt(MLAsrCaptureConstants.ASR_ERROR_CODE);
- // Perform troubleshooting based on the result code.
- }
- // Check whether error information is contained.
- if(bundle != null && bundle.containsKey(MLAsrCaptureConstants.ASR_ERROR_MESSAGE)){
- String errorMsg = bundle.getString(MLAsrCaptureConstants.ASR_ERROR_MESSAGE);
- // Perform troubleshooting based on the error information.
- }
- // Check whether a sub-result code is contained.
- if(bundle != null && bundle.containsKey(MLAsrCaptureConstants.ASR_SUB_ERROR_CODE)) {
- int subErrorCode = bundle.getInt(MLAsrCaptureConstants.ASR_SUB_ERROR_CODE);
- // Process the sub-result code.
- }
- }
- default:
- break;
- }
- }
- }