This document describes only the definition of APIs. For details about how and when to call these APIs, please refer to Automatic Speech Recognition.
Add the following configuration to the features attribute in the manifest.json file:
{"name": "service.ml.asr"} Add the following configuration to the script element on the page where the API will be called:
import mlasr from '@service.ml.asr'
Or
var mlasr = require("@service.ml.asr") Item | Description |
|---|---|
Applicable devices | Mobile phones and tablets |
Applicable regions | Global |
Description
Starts speech recognition and obtains the recognition result from the callback.
When no speech is recognized, the API automatically stops speech recognition.
Parameters
Parameter | Type | Mandatory | Description |
|---|---|---|---|
appId | string | Yes | Value of app_id in Obtaining the agconnect-services.json File. |
apiKey | string | Yes | Value of api_key in Obtaining the agconnect-services.json File. You need to set and keep your API key properly. For details, please refer to Notes on Using Cloud Authentication Information |
language | string | No | Language to be recognized. If this parameter is not set, Chinese is recognized by default. The options are as follows:
|
feature | number | No | Mode for generating the text recognition result. If this parameter is not set, texts are generated along with the speech. The options are as follows:
|
callback(eventName, ...data) | function | Yes | Callback method, which is called back for multiple times. One or more parameters are returned. The first parameter is the event name, and other parameters depend on the service. |
fail() | function | No | Callback method, which is called after an error occurs. |
Returns
Parameter | Type | Description |
|---|---|---|
eventName | string | Name of a callback event.
|
...data | 0 or a set of parameters | The value can be 0 or a set of parameters, depending on the value of eventName.
|
VoiceDataReceived parameters
Parameter | Type | Description |
|---|---|---|
data[0] | string | Audio stream. |
data[1] | string | Audio power. |
data[2] | object | Extended parameter, which is reserved currently. |
RecognizingResults parameters
Result parameters
Status parameters
Parameter | Type | Description |
|---|---|---|
data[0] | string | Status. |
data[1] | object | Extended parameter, which is reserved currently. |
Result codes returned by fail
Result Code | Description | Suggestion |
|---|---|---|
11202 | Network error. | - |
11203 | Service unavailable. | Modify parameters based on the returned error information. |
11204 | Speech unrecognized. | - |
Sample code
mlasr.start({ // Start speech recognition.
appId: '123456', // The value is of the string type. The parameter is mandatory.
apiKey: '', // The value is of the string type. The parameter is mandatory and is set for authentication.
certFingerprint: 'test', // Signature authentication information. This parameter is optional. The value is of the string type.
language: 'zh-CN', // Parameter of the string type, which is optional. This parameter is used to set the languages supported in recognition. By default, Chinese is recognized. Options: zh-CN: Chinese; en-US: English; fr-FR: French; es-ES: Spanish; de-DE: German.
feature: 11, // Parameter of the number type, which is optional. This parameter is used to set the return mode of the recognition result. Options: 11: The onRecognizingResults API is called to return texts along with speech recognition. 12: After the recognition is complete, texts are returned through onResults. The default value is 11.
callback: (eventName, ...data) => // Callback method. One or more parameters are returned. The first parameter is the event name (eventName). Other parameters depend on the service.
console.log(eventName, ...data);
switch (eventName) {
case'onStartListening': // The recorder starts to receive speech.
break;
case'onStartingOfSpeech':// The user starts to speak, that is, the speech recognizer detects that the user starts to speak.
break;
case'onVoiceDataReceived':// Return the original PCM stream and audio power to the user.
let voice = data[0], energy = data[1];
break;
case'onRecognizingResults':// Receive the recognized text from MLAsrRecognizer.
let partialResults = data[0];
break;
case'onResults':// Text data of speech recognition.
let results = data[0];
break;
case'onState':// Notify the app status change.
let state = data[0];
break;
}
},
fail: (errMsg, errCode) => { // Callback upon failure.
console.log("error:", errMsg);
},
});