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
- Install DashScope SDK version 2.22.5 or later.
- Get an API key.
- Set the API key as an environment variable:
- Review the Qwen-LiveTranslate model overview for supported languages and voices.
Quick start
Connect, send audio, and receive translated text:
Request parameters
OmniRealtimeParam
Build these with OmniRealtimeParam.builder() to establish the WebSocket connection.
Click to view the sample code
Click to view the sample code
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.
Click to view the sample code
Click to view the sample code
Parameter | Type | Required | Description |
|---|---|---|---|
|
| No | Output modalities. Default: |
|
| No | The voice for synthesized speech. The default voice for |
|
| 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().
Click to view the sample code
Click to view the sample code
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. |
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.
Sample code for real-time microphone translation
Sample code for real-time microphone translation
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