This topic describes the key interfaces and request parameters for calling Qwen-TTS-Realtime models with the DashScope Java SDK.
Supported regions: Singapore and China North 2 (Beijing). Use an API key and endpoint from the region you call; see Regions and endpoints.
Requires DashScope Java SDK 2.22.7 or later.
For more samples, see the GitHub repository.
Configure the following request parameters with the chaining methods or setters of a
Configure the following request parameters with the chaining methods or setters of a
To import:
Example 1 (Signature):
Example 2 (Signature):
Example 3 (Signature):
Example 4 (Signature):
Example 5 (Signature):
Example 6 (Signature):
Example 7 (Signature):
Example 8 (Signature):
Example 9 (Signature):
Example 10 (Signature):
Example 1 (Method):
Example 2 (Method):
Example 3 (Method):
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 | wss://ap-southeast-1.maas.qwencloudapi.com/api-ws/v1/realtime (Singapore). For another region, replace ap-southeast-1 in the URL. See Regions. |
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 Auto.
|
| mode | String | No | The interaction mode. Valid values:
|
| format | String | No | The format of the audio output from the model. Supported formats:
|
| sampleRate | int | No | The sample rate of the audio output from the model, in Hz. Supported sample rates:
|
| 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]. |
| volume | int | No | The volume of the audio. Valid range: [0, 100]. |
| pitchRate | float | No | The pitch of the synthesized audio. Default value: 1.0. Valid range: [0.5, 2.0]. |
| 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 (response_format) is set to opus.Default value: 128. Valid range: [6, 510]. |
Key interfaces
QwenTtsRealtime class
To import:
| Method | Signature | Server events | Description |
|---|---|---|---|
| connect | See Example 1 below | session.created > Session created session.updated > Session configuration updated | Opens a WebSocket connection to the server. |
| updateSession | See Example 2 below | session.updated > Session 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 | See Example 3 below | None | Appends a text segment to the server-side input buffer. The buffer stores text until you submit it.
|
| clearAppendedText | See Example 4 below | input_text_buffer.cleared > Clears text received by the server | Clears all text in the server-side input buffer. |
| commit | See Example 5 below | input_text_buffer.committed > Commits text and triggers speech synthesis response.output_item.added > New output content appears in the response response.content_part.added > New output content added to the assistant message item response.audio.delta > Model generates audio incrementally response.audio.done > Audio generation completed response.content_part.done > Streaming of audio content for the assistant message is complete response.output_item.done > Streaming of the entire output item for the assistant message is complete response.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 | See Example 6 below | session.finished > Response completed | Stops the current task. |
| close | See Example 7 below | None | Closes the connection. |
| getSessionId | See Example 8 below | None | Returns the session ID of the current task. |
| getResponseId | See Example 9 below | None | Returns the response ID of the most recent response. |
| getFirstAudioDelay | See Example 10 below | None | Returns the latency of the first audio packet in milliseconds. |
Callback interface (QwenTtsRealtimeCallback)
| Method | Parameters | Return value | Description |
|---|---|---|---|
| See Example 1 below | None | None | Called immediately after the WebSocket connection is established. |
| See Example 2 below | 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. |
| See Example 3 below | code: WebSocket close status code. reason: Close reason. | None | Called after the server closes the connection. |