We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

Documents from this version have been archived, and will not continue to be maintained. Please use the latest version.

HMS Core GuidesML KitLanguage-related ServicesASRProcedure

ASR Procedure

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.

ASR Without Speech Pickup UI

1. Call an API to create a speech recognizer.

Collapse
Word wrap
Dark theme
Copy code
  1. // context: app context.
  2. MLAsrRecognizer mSpeechRecognizer = MLAsrRecognizer.createAsrRecognizer(context);

2. Create a speech recognition result listener callback.

Collapse
Word wrap
Dark theme
Copy code
  1. // Use the callback to implement the MLAsrListener API and methods in the API.
  2. protected class SpeechRecognitionListener implements MLAsrListener {
  3.    @Override
  4.    public void onStartListening() {
  5.            // The recorder starts to receive speech.
  6.    }
  7.    @Override
  8.    public void onStartingOfSpeech() {
  9.            // The user starts to speak, that is, the speech recognizer detects that the user starts to speak.
  10.    }
  11.    @Override
  12.    public void onVoiceDataReceived(byte[] data, float energy, Bundle bundle) {
  13.            // Return the original PCM stream and audio power to the user.
  14.    }
  15.    @Override
  16.    public void onRecognizingResults(Bundle partialResults) {
  17.            // Receive the recognized text from MLAsrRecognizer.
  18.    }
  19.    @Override
  20.    public void onResults(Bundle results) {
  21.            // Text data of ASR.
  22.    }
  23.    @Override
  24.    public void onError(int error, String errorMessage) {
  25.            // Called when an error occurs in recognition.
  26.    }
  27.    @Override
  28.    public void onState(int state) {
  29.           // Notify the app status change.
  30.    }
  31. }

3. Bind the new result listener callback to the speech recognizer.

Collapse
Word wrap
Dark theme
Copy code
  1. mSpeechRecognizer.setAsrListener(new SpeechRecognitionListener());

4. Set recognition parameters and callstartRecognizingto start speech recognition.

Collapse
Word wrap
Dark theme
Copy code
  1. // Create an Intent to set parameters.
  2. Intent mSpeechRecognizerIntent = new Intent(MLAsrConstants.ACTION_HMS_ASR_SPEECH);
  3. // Use Intent for recognition parameter settings.
  4. mSpeechRecognizerIntent
  5. // 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
  6.    .putExtra(MLAsrConstants.LANGUAGE, "zh")
  7.    // 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:
  8.    // MLAsrConstants.FEATURE_WORDFLUX: Recognizes and returns texts through onRecognizingResults.
  9.    // MLAsrConstants.FEATURE_ALLINONE: After the recognition is complete, texts are returned through onResults.
  10.    .putExtra(MLAsrConstants.FEATURE, MLAsrConstants.FEATURE_WORDFLUX);
  11. // Start speech recognition.
  12. mSpeechRecognizer.startRecognizing(mSpeechRecognizerIntent);

5. Release resources after the detection is complete.

Collapse
Word wrap
Dark theme
Copy code
  1. if (mSpeechRecognizer!= null) {
  2.    mSpeechRecognizer.destroy();
  3. }

ASR with Speech Pickup UI

Create an Intent to set parameters.

Collapse
Word wrap
Dark theme
Copy code
  1. // Use Intent for recognition settings.
  2. Intent intent = new Intent(this, MLAsrCaptureActivity.class)
  3. // 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
  4.    .putExtra(MLAsrCaptureConstants.LANGUAGE, "en-US")
  5. // Set whether to display text on the speech pickup UI. MLAsrCaptureConstants.FEATURE_ALLINONE: no; MLAsrCaptureConstants.FEATURE_WORDFLUX: yes.
  6.    .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.

Collapse
Word wrap
Dark theme
Copy code
  1. // 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.
  2. startActivityForResult(intent, 100);

3. Overwrite the onActivityResult method to process the result returned by ASR.

Collapse
Word wrap
Dark theme
Copy code
  1. @Override
  2. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  3.    super.onActivityResult(requestCode, resultCode, data);
  4.    String text = "";
  5. // 100: request code between the current activity and speech pickup UI activity defined in step 2.
  6.    if (requestCode == 100) {
  7.        switch (resultCode) {
  8. // MLAsrCaptureConstants.ASR_SUCCESS: Recognition is successful.
  9.            case MLAsrCaptureConstants.ASR_SUCCESS:
  10.                if (data != null) {
  11.                    Bundle bundle = data.getExtras();
  12. // Obtain the text information recognized from speech.
  13.                    if (bundle != null && bundle.containsKey(MLAsrCaptureConstants.ASR_RESULT)) {    
  14.                        text = bundle.getString(MLAsrCaptureConstants.ASR_RESULT);
  15. // Process the recognized text information.
  16.                    }
  17.                }
  18.                break;
  19. // MLAsrCaptureConstants.ASR_FAILURE: Recognition fails.
  20.            case MLAsrCaptureConstants.ASR_FAILURE:
  21. // Processing logic for recognition failure.
  22.                if(data != null) {
  23.                    Bundle bundle = data.getExtras();
  24. // Check whether a result code is contained.
  25.                    if(bundle != null && bundle.containsKey(MLAsrCaptureConstants.ASR_ERROR_CODE)) {
  26.                        int errorCode = bundle.getInt(MLAsrCaptureConstants.ASR_ERROR_CODE);
  27. // Perform troubleshooting based on the result code.
  28.                    }
  29. // Check whether error information is contained.
  30.                    if(bundle != null && bundle.containsKey(MLAsrCaptureConstants.ASR_ERROR_MESSAGE)){
  31.                        String errorMsg = bundle.getString(MLAsrCaptureConstants.ASR_ERROR_MESSAGE);
  32. // Perform troubleshooting based on the error information.
  33.                    }
  34. // Check whether a sub-result code is contained.
  35.                    if(bundle != null && bundle.containsKey(MLAsrCaptureConstants.ASR_SUB_ERROR_CODE)) {
  36.                        int subErrorCode = bundle.getInt(MLAsrCaptureConstants.ASR_SUB_ERROR_CODE);
  37. // Process the sub-result code.
  38.                    }
  39.                }
  40.            default:
  41.                break;
  42.        }
  43.    }
  44. }


This page may contain third-party content. For details, click here.