Skip to main content
CosyVoice SDK

Java SDK

CosyVoice voice cloning Java SDK reference (VoiceEnrollmentService).

CosyVoice voice cloning can be called via the DashScope Java SDK using the VoiceEnrollmentService class. This SDK covers voice cloning only — CosyVoice voice design and all Qwen voice cloning/design must use the HTTP API. User guide: Voice cloning.

Prerequisites

Service URL

Set the base URL before creating the service:
import com.alibaba.dashscope.common.Constants;

Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";

VoiceEnrollmentService class

Package: com.alibaba.dashscope.audio.ttsv2.enrollment.VoiceEnrollmentService Manages the lifecycle of CosyVoice cloned voices (create, list, query, update, delete).

Constructor

public VoiceEnrollmentService(String apiKey)
ParameterTypeDescription
apiKeyStringYour DashScope API key.

createVoice()

Create a cloned voice from audio.
public Voice createVoice(String targetModel, String prefix, String url,
                         VoiceEnrollmentParam customParam)
    throws NoApiKeyException, InputRequiredException
ParameterTypeRequiredDescription
targetModelStringYesSpeech synthesis model for the cloned voice. Must match the model in your synthesis calls.
prefixStringYesVoice name prefix. Alphanumeric only, max 10 characters.
urlStringYesAudio file URL for cloning. Must be publicly accessible.
customParamVoiceEnrollmentParamNoOptional parameters (languageHints, maxPromptAudioLength, etc.). See VoiceEnrollmentParam.
Returns: Voice object. Call getVoiceId() to get the voice ID.

listVoice()

List cloned voices with optional filtering and pagination.
public Voice[] listVoice(String prefix, int pageIndex, int pageSize)
    throws NoApiKeyException, InputRequiredException
ParameterTypeRequiredDescription
prefixStringNoFilter voices by name prefix.
pageIndexintNoPage number, starting from 0.
pageSizeintNoResults per page.
Returns: Voice[] — array of voice objects.

queryVoice()

Query details of a specific cloned voice.
public Voice queryVoice(String voiceId)
    throws NoApiKeyException, InputRequiredException
ParameterTypeRequiredDescription
voiceIdStringYesThe voice ID to query.
Returns: Voice object with status, target model, and other details.

updateVoice()

Update a cloned voice with new audio.
public void updateVoice(String voiceId, String url, VoiceEnrollmentParam customParam)
    throws NoApiKeyException, InputRequiredException
ParameterTypeRequiredDescription
voiceIdStringYesThe voice ID to update.
urlStringYesNew audio file URL. Must be publicly accessible.
customParamVoiceEnrollmentParamNoOptional parameters.

deleteVoice()

Delete a cloned voice.
public void deleteVoice(String voiceId)
    throws NoApiKeyException, InputRequiredException
ParameterTypeRequiredDescription
voiceIdStringYesThe voice ID to delete.

Sample code

Create a 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.common.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collections;

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

  public static void main(String[] args) {
    Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
    String apiKey = System.getenv("DASHSCOPE_API_KEY");
    String targetModel = "cosyvoice-v3-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)
          .languageHints(Collections.singletonList("zh"))
          // .maxPromptAudioLength(10.0f)
          // .parameter("enable_preprocess", 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);
    }
  }
}

List voices

This example requires 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.common.Constants;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
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");
  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 {
    Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
    VoiceEnrollmentService service = new VoiceEnrollmentService(apiKey);
    // List voices
    Voice[] voices = service.listVoice(prefix, 0, 10);
    logger.info("List successful. Request ID: {}", service.getLastRequestId());
    logger.info("Voices Details: {}", new Gson().toJson(voices));
  }
}

Retrieve a specific voice

This example requires 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.common.Constants;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
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");
  private static String voiceId = "cosyvoice-v3-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 {
    Constants.baseHttpApiUrl = "https://dashscope-intl.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));
  }
}

Update a voice

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

public class Main {
  public static String apiKey = System.getenv("DASHSCOPE_API_KEY");
  private static String fileUrl = "https://your-audio-file-url"; // Replace with your actual value
  private static String voiceId = "cosyvoice-v3-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 {
    Constants.baseHttpApiUrl = "https://dashscope-intl.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 a voice

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

public class Main {
  public static String apiKey = System.getenv("DASHSCOPE_API_KEY");
  private static String voiceId = "cosyvoice-v3-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 {
    Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
    VoiceEnrollmentService service = new VoiceEnrollmentService(apiKey);
    // Delete the voice
    service.deleteVoice(voiceId);
    logger.info("Deletion submitted. Request ID: {}", service.getLastRequestId());
  }
}

VoiceEnrollmentParam class

Package: com.alibaba.dashscope.audio.ttsv2.enrollment.VoiceEnrollmentParam Use the Builder pattern to construct optional parameters for createVoice() and updateVoice().
Builder methodTypeDescription
languageHints(List<String>)List<String>Language hint for the audio. Only the first element is used. Default: ["zh"].
maxPromptAudioLength(Float)FloatMax audio duration (seconds) after preprocessing. Range: [3.0, 30.0]. Default: 10.0.
parameter(String, Object)ObjectSet additional parameters, e.g. parameter("enable_preprocess", false).

enable_preprocess

ParameterTypeRequiredDescription
enable_preprocessbooleanNoEnable audio preprocessing (noise reduction, enhancement, volume normalization). Recommended for noisy audio; disable for clean audio. Default: false.