Use the DashScope Java SDK to create and manage Qwen-Audio and CosyVoice cloned voices.
API endpoint
The SDK API endpoint must be set to the address below (including WorkspaceId) before initialization. To switch to another region, modify Constants.baseHttpApiUrl to the URL of the corresponding region.
- API Keys differ across regions; use the API Key of the corresponding region when making calls.
- Region configuration is a global setting that affects all DashScope SDK API calls.
- Singapore
- China (Beijing)
https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1When making calls, replace {WorkspaceId} with your actual Workspace ID.VoiceEnrollmentService class
Package path: com.alibaba.dashscope.audio.ttsv2.enrollment.VoiceEnrollmentService
Feature: Manages the lifecycle of Qwen-Audio-TTS, Qwen-Audio-Realtime, and CosyVoice cloned voices (create, query, update, delete).
Constructor
| Parameter | Type | Description |
|---|---|---|
| apiKey | String | API Key |
createVoice() - Create a voice
Method signature:
| Parameter | Type | Required | Description |
|---|---|---|---|
| targetModel | String | Yes | The target model that drives the voice. It must match the model used later with this voice; otherwise, the call fails. |
| prefix | String | Yes | The prefix of the voice name. Only digits and English letters are allowed, up to 10 characters. The generated voice name format: {target_model}-{prefix}-{unique-identifier}. |
| url | String | Yes | The reference audio used for voice cloning. It supports a publicly accessible URL or a Base64 Data URI, such as data:audio/wav;base64,.... |
| customParam | VoiceEnrollmentParam | No | Custom parameters. You can use the parameter() method to specify parameters such as language_hints and max_prompt_audio_length. |
Voice object. Use the getVoiceId() method to get the voice ID.
listVoice() - Query the voice list
Method signature:
| Parameter | Type | Required | Description |
|---|---|---|---|
| prefix | String | No | Filter by voice name prefix. |
| pageIndex | int | No | Page index, starting from 0. |
| pageSize | int | No | Number of entries per page. |
Voice[] objects. If getTargetModel() of a list item returns null, you can read the target model ID via voice.getData().get("target_model").
queryVoice() - Query voice details
Method signature:
| Parameter | Type | Required | Description |
|---|---|---|---|
| voiceId | String | Yes | The voice ID to query. |
Voice object. Use getTargetModel() to get the target model ID.
updateVoice() - Update a voice
Method signature:
| Parameter | Type | Required | Description |
|---|---|---|---|
| voiceId | String | Yes | The voice ID to update. |
| url | String | Yes | The new reference audio. Supports a publicly accessible URL or a Base64 Data URI, such as data:audio/wav;base64,.... |
| customParam | VoiceEnrollmentParam | No | Custom parameters. |
deleteVoice() - Delete Voice
Method signature:
| Parameter | Type | Required | Description |
|---|---|---|---|
| voiceId | String | Yes | The voice ID to delete. |
VoiceEnrollmentParam class
Package path: com.alibaba.dashscope.audio.ttsv2.enrollment.VoiceEnrollmentParam
Build the parameter object using the Builder pattern.
| Methods | Type | Description |
|---|---|---|
| model(String) | String | The voice cloning model, fixed as "voice-enrollment". |
| parameter(String, Object) | Object | Set custom parameters, such as parameter("language_hints", Arrays.asList("zh")), parameter("max_prompt_audio_length", 10.0f), parameter("enable_preprocess", false), and parameter("enable_volume_normalization", "false"). The length of the language_hints array must be 1. For optional parameters and the models that support them, see HTTP API Reference. |
Extended parameters
| Parameter Name | Type | Required | Description |
|---|---|---|---|
| enable_preprocess | boolean | No | Whether to enable audio preprocessing (noise reduction, audio enhancement, and volume normalization). It is recommended to enable it when there is background noise, and to disable it in quiet environments to preserve the voice timbre as much as possible.Default value: false. Applies to qwen-audio-3.1-tts-plus, qwen-audio-3.1-tts-flash, qwen-audio-3.0-tts-plus, qwen-audio-3.0-tts-flash, qwen-audio-3.1-realtime-plus, qwen-audio-3.1-realtime-flash, qwen-audio-3.0-realtime-plus, qwen-audio-3.0-realtime-flash, cosyvoice-v3.5-plus, cosyvoice-v3.5-flash, and cosyvoice-v3-flash. |
| enable_volume_normalization | String | No | Whether to apply volume normalization to the sample audio used for voice cloning. The value is "true" or "false". When enabled, audio synthesized with the created voice may have a different volume than a voice created with this parameter disabled. Default value: "false". |
Sample Code
Create Voice
try block above, before calling createVoice. When updating a voice, also pass a Data URI via the url parameter of updateVoice. Replace sample.wav with the path of your local audio file.
Query the Voice List
You need to import the third-party library com.google.gson.Gson.
Query a Specific Voice
You need to import the third-party library com.google.gson.Gson.