Use the DashScope Java SDK to clone and manage Qwen-Audio-TTS/CosyVoice voices.
Service endpoint
The SDK uses the China (Beijing) endpoint by default. To switch to a different region, modify Constants.baseHttpApiUrl before initialization.
- Singapore
- China (Beijing)
https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1Replace {WorkspaceId} with your actual workspace ID.Note :
API keys differ between regions. Use the API key that corresponds to the target region.
The region setting is global and affects all DashScope SDK API calls.
VoiceEnrollmentService class
Package: com.alibaba.dashscope.audio.ttsv2.enrollment.VoiceEnrollmentService
Purpose: Manages the lifecycle of Qwen-Audio-TTS/CosyVoice cloned voices (create, list, retrieve, update, and delete).
Constructor
Parameter | Type | Description |
|---|---|---|
apiKey | String | API key |
createVoice() - Create a voice
Method signature:
Parameter | Type | Required | Description |
|---|---|---|---|
targetModel | String | Yes | The text-to-speech (TTS) model that drives the cloned voice. It must match the model you specify when calling the TTS API; otherwise, synthesis fails. |
prefix | String | Yes | A prefix for the voice name. Only alphanumeric characters are allowed, with a maximum length of 10 characters. The resulting voice name follows this format: |
url | String | Yes | The URL of the audio file for voice cloning. The URL must be publicly accessible. |
customParam | No | Custom parameters such as languageHints and maxPromptAudioLength. |
Voice object. Call getVoiceId() to retrieve the voice ID.
listVoice() - List voices
Method signature:
Parameter | Type | Required | Description |
|---|---|---|---|
prefix | String | No | Filters voices by name prefix. |
pageIndex | int | No | Page index, starting from 0. |
pageSize | int | No | Number of records per page. |
Voice[] array.
queryVoice() - Retrieve voice details
Method signature:
Parameter | Type | Required | Description |
|---|---|---|---|
voiceId | String | Yes | The ID of the voice to retrieve. |
Voice object containing the voice details.
updateVoice() - Update a voice
Method signatures:
Parameter | Type | Required | Description |
|---|---|---|---|
voiceId | String | Yes | The voice ID to update. |
url | String | Yes | The new audio file URL. |
customParam | VoiceEnrollmentParam | No | Custom parameters. |
deleteVoice() - Delete a voice
Method signature:
Parameter | Type | Required | Description |
|---|---|---|---|
voiceId | String | Yes | The voice ID to delete. |
VoiceEnrollmentParam class
Package: com.alibaba.dashscope.audio.ttsv2.enrollment.VoiceEnrollmentParam
Build parameter objects using the builder pattern.
Method | Type | Description |
|---|---|---|
model(String) | String | The voice cloning model. The value must be "voice-enrollment". |
languageHints(List<String>) | List<String> | Applies only to Qwen-Audio-TTS/CosyVoice voice cloning (when model is Helps the model identify the language of the sample audio to extract voice features more accurately and improve cloning quality. If the specified language doesn't match the actual audio language (for example, setting This parameter is an array, but the current version processes only the first element. Valid values vary by model:
Default: ["zh"]. |
maxPromptAudioLength(Float) | Float | Applies only to Qwen-Audio-TTS/CosyVoice voice cloning (when model is The maximum duration (in seconds) of the reference audio after preprocessing. Valid values: [3.0, 30.0]. Default: 10.0. |
parameter(String, Object) | Object | Sets custom parameters, such as parameter("language_hints", Arrays.asList("en")), parameter("max_prompt_audio_length", 10.0f), parameter("enable_preprocess", false), and parameter("enable_volume_normalization", "false"). |
Additional parameters
Parameter | Type | Required | Description |
|---|---|---|---|
enable_preprocess | boolean | No | Applies only to Qwen-Audio-TTS/CosyVoice voice cloning (when model is Whether to enable audio preprocessing (noise reduction, audio enhancement, and volume normalization). Enable this for recordings with background noise. Disable it for recordings in quiet environments to preserve the original voice characteristics. Default: false. |
enable_volume_normalization | String | No | Whether to normalize the volume of the sample audio used for voice cloning. Valid values are |
Sample code
Create a voice
List voices
This example requires the third-party library com.google.gson.Gson.
Retrieve a specific voice
This example requires the third-party library com.google.gson.Gson.