Translate speech in real time using the DashScope Java SDK and the qwen3.5-livetranslate-flash-realtime model. The SDK connects over WebSocket, streams audio input, and returns translated text and synthesized speech.
Before you begin
-
Set the API key as an environment variable:
# Linux / macOS export DASHSCOPE_API_KEY=<your-api-key> source ~/.bashrc # Windows set DASHSCOPE_API_KEY=<your-api-key> -
Review the Qwen-LiveTranslate model overview for supported languages and voices.
Alibaba Cloud Model Studio has released workspace-specific domains for the China (Beijing) and Singapore regions. The new dedicated domains deliver superior performance and higher stability for inference requests. We recommend migrating to the new domains:
China (Beijing): from
wss://dashscope.aliyuncs.comtowss://{WorkspaceId}.cn-beijing.maas.aliyuncs.comSingapore: from
wss://dashscope-intl.aliyuncs.comtowss://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com
{WorkspaceId} is your workspace ID, which can be found on the Workspace Details page in the Alibaba Cloud Model Studio console. The existing domain remains fully functional.
Quick start
Connect, send audio, and receive translated text:
import com.alibaba.dashscope.audio.omni.*;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.google.gson.JsonObject;
import java.util.Arrays;
// 1. Build connection parameters
OmniRealtimeParam param = OmniRealtimeParam.builder()
.model("qwen3.5-livetranslate-flash-realtime")
// The following is the Singapore region URL. Replace {WorkspaceId} with your actual workspace ID. URLs vary by region.
.url("wss://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api-ws/v1/realtime")
.apikey(System.getenv("DASHSCOPE_API_KEY"))
.build();
// 2. Define a callback to handle server events
OmniRealtimeCallback callback = new OmniRealtimeCallback() {
@Override public void onOpen() { System.out.println("Connected"); }
@Override
public void onEvent(JsonObject message) {
String type = message.get("type").getAsString();
if ("response.audio_transcript.done".equals(type)) {
System.out.println("Translation: " + message.get("transcript").getAsString());
}
}
@Override
public void onClose(int code, String reason) {
System.out.println("Closed: " + code + " " + reason);
}
};
// 3. Open a session and start streaming audio
OmniRealtimeConversation conversation = new OmniRealtimeConversation(param, callback);
conversation.connect();
// 4. Configure translation target language
OmniRealtimeConfig config = OmniRealtimeConfig.builder()
.modalities(Arrays.asList(OmniRealtimeModality.AUDIO, OmniRealtimeModality.TEXT))
.translationConfig(OmniRealtimeTranslationParam.builder()
.language("en")
.build())
.build();
conversation.updateSession(config);
// 5. Send Base64-encoded audio chunks (PCM 16 kHz, 16-bit, mono)
conversation.appendAudio(audioBase64);
// 6. End the session when done
conversation.endSession();
Request parameters
OmniRealtimeParam
Build these with OmniRealtimeParam.builder() to establish the WebSocket connection.
|
Parameter |
Type |
Required |
Description |
|
|
|
Yes |
Model name. We recommend |
|
|
|
Yes |
WebSocket endpoint. Use Replace |
|
|
|
No |
API key for authentication. If not set, configure it via the |
OmniRealtimeConfig
Build these with OmniRealtimeConfig.builder() and then call conversation.updateSession(config) after connecting.
|
Parameter |
Type |
Required |
Description |
|
|
|
No |
Output modalities. Default: |
|
|
|
No |
Voice for synthesized speech. The default voice varies by model: |
|
|
|
No |
Input audio format. Default: |
|
|
|
No |
Output audio format. Default: |
|
|
|
No |
ASR model for transcribing original speech. Set to |
|
|
|
No |
Translation settings. See OmniRealtimeTranslationParam below. |
|
|
|
No |
Whether to enable VAD (Voice Activity Detection). Default value: Set to |
OmniRealtimeTranslationParam
Build target language and custom terminology with OmniRealtimeTranslationParam.builder().
|
Parameter |
Type |
Required |
Description |
|
|
|
No |
Target language code. Default: |
|
|
|
No |
Custom terminology for domain-specific terms. |
|
|
|
No |
Term mapping: keys are source terms, values are target translations. Example: |
Key interfaces
OmniRealtimeConversation
Manages the WebSocket connection and audio streaming lifecycle.
Import: com.alibaba.dashscope.audio.omni.OmniRealtimeConversation
|
Method |
Description |
|
|
Creates a conversation instance with connection parameters and an event callback. |
|
|
Opens WebSocket connection. Triggers session.created and session.updated events. Throws |
|
|
Updates session configuration after connecting. Triggers session.updated event. Only set parameters you need; defaults apply for omitted parameters. |
|
|
Sends Base64-encoded audio chunk to the server. The server automatically detects speech boundaries and triggers translation. |
|
|
In Manual mode, commits audio previously appended to the server buffer via the |
|
|
Clears uncommitted audio data from the server buffer. Triggers input_audio_buffer.cleared event. |
|
|
Ends session gracefully. The server completes in-progress translation before sending session.finished. Throws |
|
|
Stops the task and closes WebSocket connection. |
|
|
Returns session ID. |
|
|
Returns response ID of the most recent server response. |
|
|
Returns first text delay of the most recent response (milliseconds). |
|
|
Returns first audio delay of the most recent response (milliseconds). |
OmniRealtimeCallback
Handles server events delivered over WebSocket. Extend this class and implement each method to process events.
Import: com.alibaba.dashscope.audio.omni.OmniRealtimeCallback
|
Method |
Parameters |
Description |
|
|
None |
Called when WebSocket connection is established. |
|
|
|
Called for each server event. Parse the |
|
|
|
Called when WebSocket connection closes. |
Common event types received in onEvent:
|
Event type |
Description |
|
|
Server detected speech in audio stream. |
|
|
Server detected end of speech segment. |
|
|
Source-language transcription is ready. Read |
|
|
Translated text is ready. Read |
|
|
A chunk of translated audio is available. Read |
|
|
Error occurred. Read |
Complete example
This example captures microphone audio, translates it in real time, and plays translated speech through the system speaker.
What it does:
-
Connects to Qwen-LiveTranslate over WebSocket.
-
Configures Spanish-to-English translation with custom terminology.
-
Streams microphone audio in 100 ms chunks.
-
Prints original transcription and translated text.
-
Plays translated audio through speaker.
Replace placeholders with your values:
|
Placeholder |
Description |
Example |
|
|
Model Studio API key |
|
Related topics
-
Qwen-LiveTranslate model overview -- Supported languages, voices, and capabilities
-
Server-side events reference -- Event types, JSON schemas, and error codes
-
Install DashScope SDK -- Installation and dependency setup
-
Get an API key -- Creation and management