Qwen-Audio-3.0-ASR-Flash/Fun-ASR-Flash non-real-time speech recognition HarmonyOS SDK converts speech to text.
User guide: Non-real-time speech recognition. For supported audio formats, file size limits, duration limits, and other input requirements, see Audio specifications.
Quick start
- Get an API key: Get an API key. For security, we recommend that you configure the API key in an environment variable.
- Download the SDK and run the sample code:
- Download the latest SDK bundle.
- Extract the TAR archive. Obtain the SDK in HAR format from
neonuiand add it to your project dependencies. For C++ integration, usenative/libsandnative/includein the TAR archive to obtain the dynamic libraries and header files. - Open the project in DevEco Studio. The sample code is in
DashFunAsrFlashFileTranscriberPage.ets. Replace the API key to try the feature.
Call procedure
Synchronous mode
-
Initialize the SDK
-
Configure the parameters based on your business requirements
-
Call
startFileTranscriberto send a non-real-time speech recognition request and wait for the result. -
Use
onFileTransEventCallbackto listen for theEVENT_FILE_TRANS_RESULTevent and obtain the final recognition result. -
Call
releaseto release SDK resources
Request parameters
Connection and control parameters
Pass a JSON string to the initializeFileTrans method's parameters parameter to configure these settings.
Parameter example: The following JSON string is an example and does not list all parameters. Add parameters as needed in your code:
{
"url": "wss://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation",
"apikey": "st-****",
"device_id": "my_device_id",
"service_mode": "1"
}
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
url | string | Yes | The service endpoint. Set this parameter to |
apikey | string | Yes | The API key. We recommend using a short-lived, more secure temporary API key to reduce the risk of exposing a long-lived key. |
service_mode | string | Yes | The operating mode. For non-real-time speech recognition, set this parameter to |
device_id | string | Yes | A unique string that identifies the end user. You can use an in-app user ID or a client-generated unique device identifier. This ID is mainly used for log tracing and troubleshooting. |
debug_path | string | No | The path for log files. This parameter takes effect only when you call initializeFileTrans with |
max_log_file_size | number | No | The maximum log file size in bytes. This parameter takes effect only when you call initializeFileTrans with |
Recognition parameters
Use setParams to configure nls_config parameters, or use startFileTranscriber to configure all recognition parameters.
Parameter example: The following JSON string is an example and does not list all parameters. Add parameters as needed in your code:
{
"apikey": "st-****",
"messages": [
{
"content": [
{
"input_audio": {
"data": "{YOUR_AUDIO_URL}"
},
"type": "input_audio"
}
],
"role": "user"
}
],
"nls_config": {
"format": "mp3",
"model": "qwen-audio-3.0-asr-flash"
}
}
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
apikey | string | No | If Connection and control parameters |
nls_config | object | Yes | The core speech recognition configuration object, including the model and parameters that control recognition. |
nls_config.model | string | Yes | The model name. Qwen-Audio-3.0-ASR-Flash and Fun-ASR-Flash models are supported. For details, see Supported models and regions. |
nls_config.format | string | Yes | The audio format. Set this parameter to the actual format. Supported formats include |
nls_config.sample_rate | string | No | The audio sample rate in Hz. For example, |
nls_config.vocabulary_id | string | No | The precompiled vocabulary ID. Create a vocabulary in advance and pass its ID during recognition to use its hotwords. Use this option when the vocabulary is known, relatively stable, and reused across requests. For usage instructions, see Precompiled hotwords. |
nls_config.instant_vocabulary | object | No | Instant hotwords. ImportantOnly |
nls_config.language_hints | array[string] | No | The language codes for recognition. If the language is unknown in advance, omit this parameter to let the model detect it automatically.
|
messages | array[object] | Yes | The list of messages. It contains the audio to recognize and optional conversation context to improve recognition. |
ImportantContext improves the recognition accuracy of specialized terms. For usage instructions, see Context enhancement.
Constraints: Context messages (input_text and text types) are limited to five each. If the limit is exceeded, only the five most recent messages are retained. The total context text length per turn (the sum of the user and assistant text field lengths) must not exceed 400 characters. Each character counts as one. Excess characters are truncated from the end.
ImportantWhen context is included, messages messages must follow a specific order. Arrange context messages by conversation turn. Within each turn, user (input_text type) must precede the corresponding assistant (text type). The input_audio user message containing input audio must be placed at the end of messages .
| Parameter | Type | Required | Description |
|---|---|---|---|
role | string | Yes | The message role. Valid values:
|
content | array[object] | Yes | The list of message content. See the following descriptions. |
| Parameter | Type | Required | Description |
|---|---|---|---|
type | string | Yes | The content type. Each request requires at least one
|
input_audio | object | No | Required when |
input_audio.data | string | Yes | The audio data to recognize. For supported audio formats, file size limits, duration limits, and other input requirements, see Audio specifications. The following two input methods are supported:
|
text | string | No | Required when |
Key interfaces
NativeNui
initializeFileTrans
Initialize the speech recognition SDK instance.
NoteUnlike real-time speech recognition, non-real-time (recorded-file) transcription requires initializeFileTrans with an INativeFileTransCallback callback instead of initialize.
This method blocks. Call it from a non-UI thread.
Method signaturepublic initializeFileTrans(callback: INativeFileTransCallback,
parameters: string,
level: number,
save_log: boolean = false): number
Parameters
| Parameter | Type | Description |
|---|---|---|
callback | INativeFileTransCallback | The implementation of the file transcription event and data callback interface. |
parameters | string | A JSON string containing authentication, connection, and debugging parameters. See Connection and control parameters. |
level | number | The SDK log level. Use a value from the enumeration. |
save_log | boolean | Whether to save logs locally. If set to |
Returns an error code. See Error codes.
setParams
Use this method to set or update nls_config separately. You do not need to call this method if all parameters are provided in startFileTranscriber in a single call.
public setParams(params: string): number
Parameters
| Parameter | Type | Description |
|---|---|---|
params | string | Recognition parameters in |
Returns an error code. See Error codes.
startFileTranscriber
Start recognition.
Method signaturepublic startFileTranscriber(params: string, task_id: ArrayBuffer): number
Parameters
| Parameter | Type | Description |
|---|---|---|
params | string | Recognition parameters. |
task_id | ArrayBuffer | The task ID buffer. The SDK writes an internally generated random task ID string to this buffer. Its byte length must be >= 33 bytes (the example uses |
Returns an error code. See Error codes.
queryFileTranscriber
Use this method to query the current status and result of an asynchronous task. After a successful call, the result is returned through the onFileTransEventCallback callback's EVENT_FILE_TRANS_QUERY_RESULT event.
public queryFileTranscriber(task_id: string): number
Parameters
| Parameter | Type | Description |
|---|---|---|
task_id | string | The ID of the task to query (written to the buffer by |
Returns an error code. See Error codes.
cancelFileTranscriber
Immediately cancel the current task.
Method signaturepublic cancelFileTranscriber(task_id: string): number
Parameters
| Parameter | Type | Description |
|---|---|---|
task_id | string | The ID of the task to cancel. |
Returns an error code. See Error codes.
release
Release all internal SDK resources. After this call, the SDK instance becomes unavailable. To use it again, call initializeFileTrans to initialize it again.
Method signaturepublic release(): number
Return value
Returns an error code. See Error codes.
GetVersion
Get the current SDK version.
Method signaturepublic GetVersion(): string
Return value
The current SDK version.
INativeFileTransCallback: Callbacks
onFileTransEventCallback: Listen for events and recognition results
Method signatureonFileTransEventCallback: (event: Constants.NuiEvent, resultCode: number, finish: number,
asrResult: AsrResult, taskId: string) => void;
Parameters
| Parameter | Type | Description |
|---|---|---|
event | Constants.NuiEvent | The callback event. |
resultCode | number | Error code, valid when an EVENT_ASR_ERROR event occurs. |
finish | number | Whether the task has finished. |
asrResult | AsrResult | The speech recognition result. |
taskId | string | The task ID. |
onFileTransLogTrackCallback: Listen for tracing logs
This callback receives detailed internal SDK logs for troubleshooting and debugging.
To use this callback, download the HarmonyOS SDK package dated 20260908 or later.
onFileTransLogTrackCallback?: (level: Constants.LogLevel, log: string) => void;
Event types
In the HarmonyOS SDK, event types are defined by the Constants.NuiEvent enumeration. The following events are related to recorded-file transcription:
| Event | Description |
|---|---|
EVENT_FILE_TRANS_CONNECTED | Successfully connected to the service. |
EVENT_FILE_TRANS_UPLOADED | The audio file was uploaded successfully. |
EVENT_FILE_TRANS_QUERY_RESULT | The task query result. |
EVENT_FILE_TRANS_RESULT | The final recognition result. |
EVENT_ASR_ERROR | An error occurred during speech recognition. |