API reference
The mobile NUI SDK recognizes short audio clips of up to 60 seconds for scenarios such as chat, voice commands, voice input, and voice search.
Feature overview
The NUI SDK provides unified interfaces and state management for end-to-end voice capabilities and can also be used for individual voice capabilities. This topic describes the interaction flow, initialization parameters, recognition parameters, and response fields for short sentence recognition. For SDK integration and complete examples, see Android SDK and iOS SDK.
Limitations
Input audio must be 16-bit mono PCM.
The sample rate must be 8000 Hz or 16000 Hz and match the model configured for the project.
Audio for a single recognition task must not exceed 60 seconds.
Language and dialect models are configured for the project in the console, not in recognition code. For instructions, see Manage projects. Intermediate results, punctuation prediction, and inverse text normalization (ITN) are optional and can be enabled as needed.
Endpoints
The SDK uses the public endpoint in China (Shanghai) by default. ECS instances can access the internal endpoint over a VPC in the same region without incurring public outbound data transfer fees for the ECS instance. The classic network does not support access to the speech service through AnyTunnel; use a VPC for internal access.
|
Region |
Public endpoint |
Internal endpoint |
|
China (Shanghai) |
|
|
|
China (Beijing) |
|
|
|
China (Shenzhen) |
|
|
Interaction flow
The following diagram shows the interaction flow for the iOS and Android SDKs.
The header.task_id field in recognition responses uniquely identifies the task. Record this value for troubleshooting.
1. Authenticate and initialize
The client uses a valid NLS token to establish a WebSocket connection with the server. For instructions, see Obtain a token. For initialization parameters, see the initialization parameter table.
2. Start recognition
Before starting recognition, use the parameter-setting API to configure recognition parameters in JSON format. Parameters generally need to be set only once; set them again to update the token or change the configuration. For details, see the recognition parameter table.
3. Send audio
The client continuously sends audio data. When enable_intermediate_result is true, the SDK reports intermediate results through the EVENT_ASR_PARTIAL_RESULT event in the onNuiEventCallback callback. When the parameter is false, no intermediate results are returned.
Disabling intermediate results does not suppress completion or failure events. When voice activity detection (VAD) is enabled, the server can detect the end of speech and complete recognition while audio is being sent.
4. Complete recognition
Manual completion: The client sends a stop request to notify the server that all audio has been sent. The server ends the task and returns the final result.
-
Automatic completion with VAD: When
enable_voice_detectionis enabled and the end-of-speech silence reaches the condition specified bymax_end_silence, the server returnsRecognitionCompletedand ends the task. Subsequent audio is not recognized.NoteThe last intermediate result may differ from the final result. Use the result in the
EVENT_ASR_RESULTevent.
Parameters
Initialization parameters
The following table lists common initialization parameters. For complete platform-specific initialization settings and API calls, see Android SDK and iOS SDK.
|
Parameter |
Type |
Required |
Description |
|
|
String |
Yes |
The working directory from which the SDK reads configuration files. |
|
|
String |
Yes |
The Appkey of the project created in the console. |
|
|
String |
Yes |
A valid, unexpired NLS token. Set it during initialization or update it through the parameter-setting API. |
|
|
String |
Yes |
A unique device identifier, such as a MAC address, SN, or UniquePsuedoID. |
|
|
String |
No |
The debug directory. When |
|
|
String |
No |
Whether to save audio for debugging. This parameter takes effect only when |
Recognition parameters
Pass the following JSON fields through the parameter-setting API.
|
Parameter |
Type |
Required |
Description |
|
|
String |
No |
The project Appkey, usually set during initialization. |
|
|
String |
No |
Set this parameter to update the NLS token. |
|
|
Int |
Yes |
The speech service type. Set to |
|
|
String |
No |
An IP address resolved by the client for accessing the service. |
|
|
JsonObject |
No |
The speech recognition configuration object. |
Speech recognition configuration
The nls_config object contains the following fields. PCM input requirements and the transmission encoding specified by sr_format refer to different configurations.
|
Parameter |
Type |
Required |
Description |
|
|
String |
No |
The audio transmission encoding: OPUS or raw PCM. Default: OPUS. At a sample rate of 8000 Hz, only PCM is supported. |
|
|
Integer |
No |
The audio sample rate. Default: 16000 Hz. Configure a model that supports the sample rate and scenario for the project in the console. |
|
|
Boolean |
No |
Whether to return intermediate recognition results. Default: |
|
|
Boolean |
No |
Whether to add punctuation during post-processing. Default: |
|
|
Boolean |
No |
Whether to enable inverse text normalization (ITN) to convert Chinese numerals to Arabic numerals. Default: |
|
|
String |
No |
The custom language model ID. |
|
|
String |
No |
The custom hotword vocabulary ID. |
|
|
Boolean |
No |
Whether to enable voice activity detection (VAD) to detect the start and end of speech. Default: |
|
|
Integer |
No |
The maximum initial silence duration, in milliseconds. Takes effect only when |
|
|
Integer |
No |
The maximum ending silence duration, in milliseconds. Valid values: 200 to 6000. Takes effect only when |
|
|
JsonObject |
No |
Extended configuration for parameters supported by the interaction protocol but not listed in this table. |
Response
The following JSON examples show the format of recognition result messages.
Intermediate result
{
"header": {
"namespace": "SpeechRecognizer",
"name": "RecognitionResultChanged",
"status": 20000000,
"message_id": "e06d2b5d50ca40d5a50d4215c7c8****",
"task_id": "4c3502c7a5ce4ac3bdc488749ce4****",
"status_text": "Gateway:SUCCESS:Success."
},
"payload": {
"result": "The weather in Beijing"
}
}
Final result
{
"header": {
"namespace": "SpeechRecognizer",
"name": "RecognitionCompleted",
"status": 20000000,
"message_id": "10490c992aef44eaa4246614838f****",
"task_id": "4c3502c7a5ce4ac3bdc488749ce4****",
"status_text": "Gateway:SUCCESS:Success."
},
"payload": {
"result": "The weather in Beijing."
}
}
Response fields
header
|
Field |
Type |
Description |
|
|
String |
The message namespace: |
|
|
String |
The message name. |
|
|
Integer |
The service status code, which indicates whether the request succeeded. |
|
|
String |
The message ID, automatically generated by the SDK. |
|
|
String |
The globally unique task ID, used for troubleshooting. |
|
|
String |
The status message. |
payload
|
Field |
Type |
Description |
|
|
String |
Recognized text: an intermediate result in |
Error codes
For service and mobile SDK error codes and troubleshooting, see Error codes.