Use the DashScope Java SDK to call Qwen-ASR-Realtime.
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.
Quick start
your_audio_file.pcm (PCM16, 16 kHz, mono). If you only have an MP3, WAV, or similar format, convert it with ffmpeg:Request parameters
-
The following parameters are set with the chained methods of
OmniRealtimeParam.Click to view sample code
Parameter
Type
Required
Description
modelStringYes
The name of the model to use.
urlStringYes
Speech recognition service endpoint:
China (Beijing):
wss://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api-ws/v1/realtime. Replace{WorkspaceId}with your actual Workspace ID when calling.Singapore:
wss://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api-ws/v1/realtime. Replace{WorkspaceId}with your actual workspace ID when calling.
apikeyStringNo
Set the API key.
-
The following parameters are set with the chained methods of
OmniRealtimeConfig.Click to view sample code
Parameter
Type
Required
Description
modalitiesList<OmniRealtimeModality>Yes
The output modality of the model, fixed to
[OmniRealtimeModality.TEXT].enableTurnDetectionbooleanNo
Whether to enable server-side voice activity detection (VAD). When disabled, you must call the
commit()method to trigger recognition.Default:
true.Valid values:
true: enabledfalse: disabled
turnDetectionTypeStringNo
Server-side VAD type, fixed to
server_vad.turnDetectionThresholdfloatNo
The VAD detection threshold. We recommend setting this value to
0.0.Default:
0.5.Valid range:
[-1, 1].A lower threshold increases the sensitivity of the VAD, which may cause background noise to be misclassified as speech. A higher threshold reduces sensitivity, which helps reduce false triggers in noisy environments.
turnDetectionSilenceDurationMsintNo
The VAD segmentation threshold (in ms). Silence lasting longer than this threshold is treated as the end of an utterance. We recommend setting this value to
400.Default:
800.Valid range:
[200, 6000].A lower value (such as 300 ms) allows the model to respond faster, but may cause unreasonable segmentation 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 configuration.
-
The following parameters are set with the
settermethods ofOmniRealtimeTranscriptionParam.Click to view 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
The audio sampling rate (Hz). Supports
16000and8000.Default:
16000.When set to
8000, the server first upsamples the audio to 16,000 Hz before recognition, which may introduce minor latency. We recommend using it only when the source audio is 8,000 Hz (such as telephone lines).inputAudioFormatStringNo
The audio format. Supports
pcmandopus.Default:
pcm.
API reference
OmniRealtimeConversation class
OmniRealtimeConversation is imported via import com.alibaba.dashscope.audio.omni.OmniRealtimeConversation;.
| Method signature | Server response events (delivered via callback) | 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. We recommend calling this method first to apply the settings after the connection is established. If this method is not called, the system uses the default configuration. You only need to focus on the parameters involved in Request parameters. | |
| None | Appends a Base64-encoded audio chunk to the cloud input audio buffer.
| |
input_audio_buffer.committedThe server received the committed audio | Commits the audio and video previously appended to the cloud buffer via append. An error occurs if the input audio buffer is empty.Disabled when: Request parameters enableTurnDetection is set to true. | |
session.finishedThe server completes speech recognition and ends the session | Notifies the server to end the session. After receiving the end-of-session notification, the server completes 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 response events and data to the client via callbacks.
Inherit this class and implement the corresponding methods to handle server events.
Import it via import com.alibaba.dashscope.audio.omni.OmniRealtimeCallback;.
| Method signature | Parameters | Description |
|---|---|---|
| None | Triggered when the WebSocket connection is successfully established. | |
| message: server event | Triggered when a server event is received. | |
| code: status codereason: log information when the WebSocket connection is closed | Triggered when the WebSocket connection is closed. |