Skip to main content

Qwen-Audio and CosyVoice Voice Cloning Java SDK Reference

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.
Alibaba Cloud Model Studio has launched workspace-specific domains for the China (Beijing) and Singapore regions, which deliver superior performance and higher stability for inference requests. Migrating to the new domain is recommended:
  • China (Beijing) region: migrate from dashscope.aliyuncs.com to {WorkspaceId}.cn-beijing.maas.aliyuncs.com
  • Singapore region: migrate from dashscope-intl.aliyuncs.com to {WorkspaceId}.ap-southeast-1.maas.aliyuncs.com

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

public VoiceEnrollmentService(String apiKey)
Parameter description:
ParameterTypeDescription
apiKeyStringAPI Key

createVoice() - Create a voice

Method signature:
public Voice createVoice(String targetModel, String prefix, String url, VoiceEnrollmentParam customParam) throws NoApiKeyException, InputRequiredException
Parameter description:
ParameterTypeRequiredDescription
targetModelStringYesThe target model that drives the voice. It must match the model used later with this voice; otherwise, the call fails.
prefixStringYesThe 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}.
urlStringYesThe reference audio used for voice cloning. It supports a publicly accessible URL or a Base64 Data URI, such as data:audio/wav;base64,....
customParamVoiceEnrollmentParamNoCustom parameters. You can use the parameter() method to specify parameters such as language_hints and max_prompt_audio_length.
Return value: a Voice object. Use the getVoiceId() method to get the voice ID.

listVoice() - Query the voice list

Method signature:
public Voice[] listVoice(String prefix, int pageIndex, int pageSize) throws NoApiKeyException, InputRequiredException
Parameter description:
ParameterTypeRequiredDescription
prefixStringNoFilter by voice name prefix.
pageIndexintNoPage index, starting from 0.
pageSizeintNoNumber of entries per page.
Return value: an array of 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:
public Voice queryVoice(String voiceId) throws NoApiKeyException, InputRequiredException
Parameter description:
ParameterTypeRequiredDescription
voiceIdStringYesThe voice ID to query.
Return value: a Voice object. Use getTargetModel() to get the target model ID.

updateVoice() - Update a voice

Method signature:
public void updateVoice(String voiceId, String url) throws NoApiKeyException, InputRequiredException
public void updateVoice(String voiceId, String url, VoiceEnrollmentParam customParam) throws NoApiKeyException, InputRequiredException
Parameter description:
ParameterTypeRequiredDescription
voiceIdStringYesThe voice ID to update.
urlStringYesThe new reference audio. Supports a publicly accessible URL or a Base64 Data URI, such as data:audio/wav;base64,....
customParamVoiceEnrollmentParamNoCustom parameters.

deleteVoice() - Delete Voice

Method signature:
public void deleteVoice(String voiceId) throws NoApiKeyException, InputRequiredException
Parameter description:
ParameterTypeRequiredDescription
voiceIdStringYesThe voice ID to delete.

VoiceEnrollmentParam class

Package path: com.alibaba.dashscope.audio.ttsv2.enrollment.VoiceEnrollmentParam Build the parameter object using the Builder pattern.
MethodsTypeDescription
model(String)StringThe voice cloning model, fixed as "voice-enrollment".
parameter(String, Object)ObjectSet 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 NameTypeRequiredDescription
enable_preprocessbooleanNoWhether 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_normalizationStringNoWhether 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

import com.alibaba.dashscope.audio.ttsv2.enrollment.Voice;
import com.alibaba.dashscope.audio.ttsv2.enrollment.VoiceEnrollmentParam;
import com.alibaba.dashscope.audio.ttsv2.enrollment.VoiceEnrollmentService;
import com.alibaba.dashscope.utils.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

public class Main {
    private static final Logger logger = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) {
        // The following configuration is for China (Beijing). Replace "{WorkspaceId}" with your actual workspace ID. Configurations vary by region
        Constants.baseHttpApiUrl = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1";
        String apiKey = System.getenv("DASHSCOPE_API_KEY");
        String targetModel = "qwen-audio-3.1-tts-plus";
        String prefix = "myvoice";
        String fileUrl = "https://your-audio-file-url";
        String cloneModelName = "voice-enrollment";

        try {
            VoiceEnrollmentService service = new VoiceEnrollmentService(apiKey);
            Voice myVoice = service.createVoice(
                    targetModel,
                    prefix,
                    fileUrl,
                    VoiceEnrollmentParam.builder()
                            .model(cloneModelName)
                            .parameter("language_hints", Arrays.asList("zh"))
                            // .parameter("max_prompt_audio_length", 10.0f)
                            // .parameter("enable_preprocess", false)
                            // .parameter("enable_volume_normalization", "false")
                            .build());

            logger.info("Voice creation submitted. Request ID: {}", service.getLastRequestId());
            logger.info("Generated Voice ID: {}", myVoice.getVoiceId());
        } catch (Exception e) {
            logger.error("Failed to create voice", e);
        }
    }
}
When using a local WAV audio file, add the following assignment inside the 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.
fileUrl = "data:audio/wav;base64," + java.util.Base64.getEncoder().encodeToString(
        java.nio.file.Files.readAllBytes(java.nio.file.Paths.get("sample.wav")));

Query the Voice List

You need to import the third-party library com.google.gson.Gson.
import com.alibaba.dashscope.audio.ttsv2.enrollment.Voice;
import com.alibaba.dashscope.audio.ttsv2.enrollment.VoiceEnrollmentService;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {
    public static String apiKey = System.getenv("DASHSCOPE_API_KEY");  // Replace with your API key if not using an environment variable
    private static String prefix = "myvoice"; // Replace with your actual value
    private static final Logger logger = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args)
            throws NoApiKeyException, InputRequiredException {
        // The following configuration is for China (Beijing). Replace "{WorkspaceId}" with your actual workspace ID. Configurations vary by region
        Constants.baseHttpApiUrl = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1";
        VoiceEnrollmentService service = new VoiceEnrollmentService(apiKey);
        // Query voices
        Voice[] voices = service.listVoice(prefix, 0, 10);
        logger.info("List successful. Request ID: {}", service.getLastRequestId());
        logger.info("Voices Details: {}", new Gson().toJson(voices));
        for (Voice voice : voices) {
            logger.info("Target model: {}", voice.getData().get("target_model"));
        }
    }
}

Query a Specific Voice

You need to import the third-party library com.google.gson.Gson.
import com.alibaba.dashscope.audio.ttsv2.enrollment.Voice;
import com.alibaba.dashscope.audio.ttsv2.enrollment.VoiceEnrollmentService;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {
    public static String apiKey = System.getenv("DASHSCOPE_API_KEY");  // Replace with your API key if not using an environment variable
    private static String voiceId = "qwen-audio-3.1-tts-plus-myvoice-xxx"; // Replace with your actual value
    private static final Logger logger = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args)
            throws NoApiKeyException, InputRequiredException {
        // The following configuration is for China (Beijing). Replace "{WorkspaceId}" with your actual workspace ID. Configurations vary by region
        Constants.baseHttpApiUrl = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1";
        VoiceEnrollmentService service = new VoiceEnrollmentService(apiKey);
        Voice voice = service.queryVoice(voiceId);

        logger.info("Query successful. Request ID: {}", service.getLastRequestId());
        logger.info("Voice Details: {}", new Gson().toJson(voice));
        logger.info("Target model: {}", voice.getTargetModel());
    }
}

Update a Voice

import com.alibaba.dashscope.audio.ttsv2.enrollment.VoiceEnrollmentService;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {
    public static String apiKey = System.getenv("DASHSCOPE_API_KEY");  // Replace with your API key if not using an environment variable
    private static String fileUrl = "https://your-audio-file-url";  // Replace with your actual value
    private static String voiceId = "qwen-audio-3.1-tts-plus-myvoice-xxx"; // Replace with your actual value
    private static final Logger logger = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args)
            throws NoApiKeyException, InputRequiredException {
        // The following configuration is for China (Beijing). Replace "{WorkspaceId}" with your actual workspace ID. Configurations vary by region
        Constants.baseHttpApiUrl = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1";
        VoiceEnrollmentService service = new VoiceEnrollmentService(apiKey);
        // Update the voice
        service.updateVoice(voiceId, fileUrl);
        logger.info("Update submitted. Request ID: {}", service.getLastRequestId());
    }
}

Delete Voice

import com.alibaba.dashscope.audio.ttsv2.enrollment.VoiceEnrollmentService;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {
    public static String apiKey = System.getenv("DASHSCOPE_API_KEY");  // Replace with your API key if not using an environment variable
    private static String voiceId = "qwen-audio-3.1-tts-plus-myvoice-xxx"; // Replace with your actual value
    private static final Logger logger = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args)
            throws NoApiKeyException, InputRequiredException {
        // The following configuration is for China (Beijing). Replace "{WorkspaceId}" with your actual workspace ID. Configurations vary by region
        Constants.baseHttpApiUrl = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1";
        VoiceEnrollmentService service = new VoiceEnrollmentService(apiKey);
        // Delete the voice
        service.deleteVoice(voiceId);
        logger.info("Deletion submitted. Request ID: {}", service.getLastRequestId());
    }
}