Key interfaces and request parameters for Qwen real-time speech synthesis DashScope Java SDK.
Prerequisites
Requires DashScope Java SDK 2.22.7 or later.
Getting started
- Server commit mode
- Commit mode
appendText()Request parameters
Configure the following request parameters with the chaining methods or setters of a QwenTtsRealtimeParam object, then pass the object to the QwenTtsRealtime constructor.
Parameter | Type | Required | Description |
|---|---|---|---|
model | String | Yes | Model name (see Supported models). |
url | String | Yes | China (Beijing): Singapore: |
QwenTtsRealtimeConfig object, then pass the object to the updateSession method.
Parameter | Type | Required | Description |
|---|---|---|---|
voice | String | Yes | The voice for speech synthesis. For more information, see Supported voices. System voices and custom voices are supported:
|
languageType | String | No | Specifies the language of the synthesized audio. The default value is
|
mode | String | No | The interaction mode. Valid values:
|
format | String | No | The format of the audio output from the model. Supported formats:
Qwen-TTS-Realtime (see Supported models) supports only |
sampleRate | int | No | The sample rate of the audio output from the model, in Hz. Supported sample rates:
Qwen-TTS-Realtime (see Supported models) supports only 24000. |
speechRate | float | No | The speech rate of the audio. A value of 1.0 is the normal speed. A value less than 1.0 is slower, and a value greater than 1.0 is faster. Default value: 1.0. Valid range: [0.5, 2.0]. Qwen-TTS-Realtime (see Supported models) does not support this parameter. |
volume | int | No | The volume of the audio. Default value: 50. Valid range: [0, 100]. Qwen-TTS-Realtime (see Supported models) does not support this parameter. |
pitchRate | float | No | The pitch of the synthesized audio. Default value: 1.0. Valid range: [0.5, 2.0]. Qwen-TTS-Realtime (see Supported models) does not support this parameter. |
bitRate | int | No | Specifies the bitrate of the audio in kbps. A higher bitrate results in better audio quality and a larger file size. This parameter is available only when the audio format ( Default value: 128. Valid range: [6, 510]. Qwen-TTS-Realtime (see Supported models) does not support this parameter. |
instructions | String | No | Sets the instructions. For more information, see Real-time speech synthesis - Qwen. Default value: None. The parameter is not active if not set. Length limit: The length cannot exceed 1600 tokens. Supported languages: Chinese and English only. Scope: This feature is available only for the Qwen3-TTS-Instruct-Flash-Realtime model series. |
optimizeInstructions | boolean | No | Specifies whether to optimize the Default value: false. Behavior: When set to true, the system enhances and rewrites the content of Scenarios: Recommended for scenarios that require high-quality, fine-grained voice expression. Dependency: This parameter depends on the Scope: This feature is available only for the Qwen3-TTS-Instruct-Flash-Realtime model series. |
Key interfaces
QwenTtsRealtime class
To import:
| Method | Signature | Server events | Description |
|---|---|---|---|
| connect | session.createdSession createdsession.updated Session configuration updated | Opens a WebSocket connection to the server. | |
| updateSession | session.updatedSession configuration updated | Updates the session configuration. See Request parameters.After connecting, the server returns default input and output configurations for the session. Call this method immediately after connect() to override defaults.The server validates parameters upon receiving a session.update event. If any parameter is invalid, the server returns an error. Otherwise, it updates the session configuration on the server side. | |
| appendText | None | Appends a text segment to the server-side input buffer. The buffer stores text until you submit it.
| |
| clearAppendedText | input_text_buffer.clearedClears text received by the server | Clears all text in the server-side input buffer. | |
| commit | input_text_buffer.committedCommits text and triggers speech synthesisresponse.output_item.added New output content appears in the responseresponse.content_part.added New output content added to the assistant message itemresponse.audio.delta Model generates audio incrementallyresponse.audio.done Audio generation completedresponse.content_part.done Streaming of audio content for the assistant message is completeresponse.output_item.done Streaming of the entire output item for the assistant message is completeresponse.done Response completed | Commits text previously appended to the server-side buffer and synthesizes all text immediately. Returns an error if the buffer is empty.
| |
| finish | session.finishedResponse completed | Stops the current task. | |
| close | None | Closes the connection. | |
| getSessionId | None | Returns the session ID of the current task. | |
| getResponseId | None | Returns the response ID of the most recent response. | |
| getFirstAudioDelay | None | Returns the latency of the first audio packet in milliseconds. |
Callback interface (QwenTtsRealtimeCallback)
| Method | Parameters | Return value | Description |
|---|---|---|---|
| None | None | Called immediately after the WebSocket connection is established. | |
| message: server-side response event. | None | Called when the server sends an event, including API call responses and model-generated audio. See Server-side events. | |
| code: WebSocket close status code.reason: Close reason. | None | Called after the server closes the connection. |