This document describes how to use the DashScope Java SDK to call the Real-time Speech Recognition (Qwen-ASR-Realtime) model.
Prerequisites
- Install the SDK and make sure the DashScope SDK version is 2.22.5 or later.
- Obtain and configure an API Key.
- Learn about the WebSocket API.
QuickStart
your_audio_file.pcm (PCM16, 16 kHz, mono). If you only have formats such as MP3/WAV, you can convert them with ffmpeg:Request parameters
-
The following parameters are set through the chained methods of
OmniRealtimeParam.Click to view the sample code
Parameter
Type
Required
Description
modelStringYes
The name of the model to use.
urlStringYes
Speech recognition service address:
China (Beijing) region:
wss://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api-ws/v1/realtime. When making a call, replace{WorkspaceId}with your actual Workspace ID.Singapore region:
wss://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api-ws/v1/realtime. When making a call, replace{WorkspaceId}with your actual Workspace ID.
apikeyStringNo
The API Key to configure.
-
The following parameters are set through the chained methods of
OmniRealtimeConfig.Click to view the sample code
Parameter
Type
Required
Description
modalitiesList<OmniRealtimeModality>Yes
The model output modality, fixed to
[OmniRealtimeModality.TEXT].enableTurnDetectionbooleanNo
Whether to enable server-side voice activity detection (VAD). When it is disabled, you must manually call the
commit()method to trigger recognition.Default value:
true.Valid values:
true: Enablefalse: Disable
turnDetectionTypeStringNo
The server-side VAD type, fixed as
server_vad.turnDetectionThresholdfloatNo
The VAD detection threshold. We recommend setting this value to
0.0.Default value:
0.5.Value range:
[-1, 1].A lower threshold increases the sensitivity of the VAD and may misclassify background noise as speech. A higher threshold reduces the sensitivity, which helps reduce false triggers in noisy environments.
turnDetectionSilenceDurationMsintNo
The VAD sentence-break detection threshold (ms). Silence lasting longer than this threshold is considered the end of an utterance. We recommend setting this value to
400.Default value:
800.Value range:
[200, 6000].A lower value (such as 300 ms) allows the model to respond faster, but may cause unreasonable sentence breaks at natural pauses. A higher value (such as 1200 ms) better handles pauses within long sentences, but increases the overall response latency.
transcriptionConfigOmniRealtimeTranscriptionParamNo
Speech recognition related configurations.
-
The following parameters are set via the
settermethods ofOmniRealtimeTranscriptionParam.Click to view the sample code
Parameter
Type
Required
Description
languageStringNo
The source language of the audio.
zh: Chinese (Mandarin, Sichuan dialect, Minnan dialect, Wu dialect)
yue: Cantonese
en: English
ja: Japanese
de: German
ko: Korean
ru: Russian
fr: French
pt: Portuguese
ar: Arabic
it: Italian
es: Spanish
hi: Hindi
id: Indonesian
th: Thai
tr: Turkish
uk: Ukrainian
vi: Vietnamese
cs: Czech
da: Danish
fil: Filipino
fi: Finnish
is: Icelandic
ms: Malay
no: Norwegian
pl: Polish
sv: Swedish
inputSampleRateintNo
Audio sampling rate (Hz). Supports
16000and8000.Default value:
16000.When set to
8000, the server first upsamples the audio to 16000 Hz before recognition, which may introduce slight latency. It is recommended only when the source audio is 8000 Hz (for example, telephone lines).inputAudioFormatStringNo
Audio format. Supports
pcmandopus.Default value:
pcm.
API reference
OmniRealtimeConversation class
OmniRealtimeConversation is imported via import com.alibaba.dashscope.audio.omni.OmniRealtimeConversation;.
| Method signature | Server response events (delivered via callbacks) | Description |
|---|---|---|
| None | Constructor. | |
session.createdSession createdsession.updated Session configuration updated | Creates a connection with the server. | |
session.updatedSession configuration updated | Used to update the session configuration. It is recommended to call this method first after the connection is established to apply settings. If this method is not called, the system uses the default configuration. Only the parameters involved in the request parameters need to be considered. | |
| None | Appends Base64-encoded audio data chunks to the cloud input audio buffer.
| |
input_audio_buffer.committedThe server received the committed audio | Commit the audio and video previously added to the cloud-side buffer via append. If the input audio buffer is empty, an error will occur.Disabled scenarios: When enableTurnDetection in request parameters is set to true. | |
session.finishedThe server completes speech recognition and ends the session. | Notifies the server to end the session. After receiving the session-end notification, the server will complete the final speech recognition.When to call:
endSessionAsync is the asynchronous version of endSession; the two are functionally identical. | |
| None | Terminates the task and closes the connection. | |
| None | Gets the session_id of the current task. | |
| None | Gets the response_id of the most recent response. |
Callback interface (OmniRealtimeCallback)
The server returns server response events and data to the client through callbacks.
Extend this class and implement the corresponding methods to handle server events.
Import it via import com.alibaba.dashscope.audio.omni.OmniRealtimeCallback;.
| Method signature | Parameter | Description |
|---|---|---|
| None | Triggered when the WebSocket connection is successfully established. | |
| message: Server events | Triggered when a server event is received. | |
| code: status codereason: Log information generated when the WebSocket connection is closed | Triggered when the WebSocket connection is closed. |