API reference

Updated at:

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)

wss://nls-gateway-cn-shanghai.aliyuncs.com/ws/v1

ws://nls-gateway-cn-shanghai-internal.aliyuncs.com:80/ws/v1

China (Beijing)

wss://nls-gateway-cn-beijing.aliyuncs.com/ws/v1

ws://nls-gateway-cn-beijing-internal.aliyuncs.com:80/ws/v1

China (Shenzhen)

wss://nls-gateway-cn-shenzhen.aliyuncs.com/ws/v1

ws://nls-gateway-cn-shenzhen-internal.aliyuncs.com:80/ws/v1

Interaction flow

The following diagram shows the interaction flow for the iOS and Android SDKs.

image

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_detection is enabled and the end-of-speech silence reaches the condition specified by max_end_silence, the server returns RecognitionCompleted and ends the task. Subsequent audio is not recognized.

    Note

    The last intermediate result may differ from the final result. Use the result in the EVENT_ASR_RESULT event.

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

workspace

String

Yes

The working directory from which the SDK reads configuration files.

app_key

String

Yes

The Appkey of the project created in the console.

token

String

Yes

A valid, unexpired NLS token. Set it during initialization or update it through the parameter-setting API.

device_id

String

Yes

A unique device identifier, such as a MAC address, SN, or UniquePsuedoID.

debug_path

String

No

The debug directory. When save_log is true during SDK initialization, this directory is used to save intermediate audio files.

save_wav

String

No

Whether to save audio for debugging. This parameter takes effect only when save_log is true during SDK initialization. Audio is saved in the directory specified by debug_path, which must exist and be writable.

Recognition parameters

Pass the following JSON fields through the parameter-setting API.

Parameter

Type

Required

Description

app_key

String

No

The project Appkey, usually set during initialization.

token

String

No

Set this parameter to update the NLS token.

service_type

Int

Yes

The speech service type. Set to 0 for short sentence recognition.

direct_ip

String

No

An IP address resolved by the client for accessing the service.

nls_config

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

sr_format

String

No

The audio transmission encoding: OPUS or raw PCM. Default: OPUS. At a sample rate of 8000 Hz, only PCM is supported.

sample_rate

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.

enable_intermediate_result

Boolean

No

Whether to return intermediate recognition results. Default: false.

enable_punctuation_prediction

Boolean

No

Whether to add punctuation during post-processing. Default: false.

enable_inverse_text_normalization

Boolean

No

Whether to enable inverse text normalization (ITN) to convert Chinese numerals to Arabic numerals. Default: false.

customization_id

String

No

The custom language model ID.

vocabulary_id

String

No

The custom hotword vocabulary ID.

enable_voice_detection

Boolean

No

Whether to enable voice activity detection (VAD) to detect the start and end of speech. Default: false.

max_start_silence

Integer

No

The maximum initial silence duration, in milliseconds. Takes effect only when enable_voice_detection is true. If no speech is detected within this duration after recognition starts, the server returns TaskFailed and ends the task.

max_end_silence

Integer

No

The maximum ending silence duration, in milliseconds. Valid values: 200 to 6000. Takes effect only when enable_voice_detection is true. When this duration is exceeded, the server returns RecognitionCompleted and ends the task. Subsequent audio is not recognized.

extend_config

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

namespace

String

The message namespace: SpeechRecognizer.

name

String

The message name. RecognitionResultChanged indicates an intermediate result; RecognitionCompleted indicates completion.

status

Integer

The service status code, which indicates whether the request succeeded.

message_id

String

The message ID, automatically generated by the SDK.

task_id

String

The globally unique task ID, used for troubleshooting.

status_text

String

The status message.

payload

Field

Type

Description

result

String

Recognized text: an intermediate result in RecognitionResultChanged, or the final result in RecognitionCompleted.

Error codes

For service and mobile SDK error codes and troubleshooting, see Error codes.