Skip to main content
CosyVoice

CosyVoice HTTP Java SDK

Use the Java SDK to call the CosyVoice speech synthesis API in non-streaming or streaming mode.

CosyVoice non-real-time speech synthesis over HTTP is available only in China North 2 (Beijing). The examples use the cn-beijing endpoint; use an API key created for that region. See Regions and endpoints.

Prerequisites

HttpSpeechSynthesizer class

Package: com.alibaba.dashscope.audio.http_tts.HttpSpeechSynthesizer Description: HTTP-based speech synthesis that supports non-streaming and streaming modes.

Constructor

public HttpSpeechSynthesizer()
Creates an HttpSpeechSynthesizer instance with the default configuration. The SDK reads the API key from the DASHSCOPE_API_KEY environment variable. If that variable is not set, it falls back to Constants.apiKey.

callAndReturnAudio() — non-streaming (returns audio data)

Method signature:
public ByteBuffer callAndReturnAudio(HttpSpeechSynthesisParam param) throws ApiException, NoApiKeyException, InputRequiredException
Parameters:
ParameterTypeDescription
paramHttpSpeechSynthesisParamThe speech synthesis parameter object that contains model, text, voice, and other configurations.
Returns: ByteBuffer containing the complete audio data. Call remaining() to get the audio size in bytes.

call() — non-streaming (returns audio URL)

Method signature:
public HttpSpeechSynthesisResult call(HttpSpeechSynthesisParam param) throws ApiException, NoApiKeyException, InputRequiredException
Parameters:
ParameterTypeDescription
paramHttpSpeechSynthesisParamThe speech synthesis parameter object.
Returns: An HttpSpeechSynthesisResult object. Call getAudioInfo().getUrl() to get the audio download URL. The URL expires after a fixed period. Call getAudioInfo().getExpiresAt() to get the expiration time.

streamCall() — streaming

Method signature:
public void streamCall(HttpSpeechSynthesisParam param, ResultCallback<HttpSpeechSynthesisResult> callback) throws ApiException, NoApiKeyException, InputRequiredException
Parameters:
ParameterTypeDescription
paramHttpSpeechSynthesisParamThe speech synthesis parameter object.
callbackResultCallback<HttpSpeechSynthesisResult>The callback object. Implement onEvent (receives audio chunks), onComplete (synthesis finished), and onError (error handling).
This method is asynchronous. Audio data arrives in chunks through the callback as synthesis progresses, which reduces first-packet latency. Use this mode for real-time playback scenarios. ResultCallback methods: com.alibaba.dashscope.common.ResultCallback is a generic callback interface provided by the DashScope SDK. Implement the following three methods:
MethodParameterDescription
onEventHttpSpeechSynthesisResult resultTriggered on each audio chunk. Call result.hasAudioData() to check for audio data and result.getAudioDataSize() to get the chunk size.
onCompleteNoneTriggered when synthesis completes and all audio chunks have been received.
onErrorException eTriggered on synthesis error. Call e.getMessage() to get the error details.

HttpSpeechSynthesisParam class

Package: com.alibaba.dashscope.audio.http_tts.HttpSpeechSynthesisParam Use the Builder pattern to construct parameter objects. Some parameters don't have dedicated Builder methods. Set them using the parameter(String key, Object value) method or the parameters(Map<String, Object>) method inherited from the parent class.
MethodTypeRequiredDescription
model(String)StringYesThe speech synthesis model. Valid values:
  • cosyvoice-v3.5-plus
  • cosyvoice-v3.5-flash
  • cosyvoice-v3-plus
  • cosyvoice-v3-flash
  • cosyvoice-v2
text(String)StringYesThe text to synthesize. SSML and LaTeX format inputs are supported. Replace the text content with the corresponding format.
voice(String)StringYesThe voice for synthesis. Valid values:
format(String)StringNoThe audio format. Default value: mp3. Valid values:
  • mp3
  • pcm
  • wav
  • opus
sampleRate(int)intNoThe audio sample rate in Hz. Valid values: 8000, 16000, 22050 (default), 24000, 44100, 48000.
volume(int)intNoThe volume level. Default value: 50. Valid values: [0, 100].
rate(float)floatNoThe speech rate. Default value: 1.0. Valid values: [0.5, 2.0].
pitch(float)floatNoThe pitch. Default value: 1.0. Valid values: [0.5, 2.0].
enable_ssmlbooleanNoEnables SSML. When set to true, pass SSML-formatted text in the text parameter. For supported SSML tags and usage, see SSML. For the SSML usage restrictions (supported models, voices, and APIs), see Limitations. Default: false. Set enable_ssml using the HttpSpeechSynthesisParam instance's parameter method or parameters method: See Example 1 below See Example 2 below
word_timestamp_enabledbooleanNoSpecifies whether to enable word-level timestamps. Default value: false. Available only in streaming output mode. Supported voices: cloned voices of cosyvoice-v3.5-plus, cosyvoice-v3.5-flash, cosyvoice-v3-flash, cosyvoice-v3-plus, cosyvoice-v2, and system voices marked as supported in CosyVoice Voice list. Cloned voices of other models do not support this feature. Set word_timestamp_enabled using the HttpSpeechSynthesisParam instance's parameter method or parameters method: See Example 3 below See Example 4 below
seedintNoThe random seed for synthesis. Different seeds produce different outputs. With identical model version, text, voice, and other parameters, the same seed reproduces the same output. Default value: 0. Valid values: [0, 65535]. Set seed using the HttpSpeechSynthesisParam instance's parameter method or parameters method: See Example 5 below See Example 6 below
language_hintsListNoTip: - This parameter is an array, but the current version processes only the first element. Pass a single value.
  • This parameter specifies the target language for speech synthesis and is unrelated to the language of the sample audio used in voice cloning. To set the source language for a voice cloning task, see the Voice Cloning API reference.
Specifies the target language for speech synthesis to improve output quality. Use this parameter when numbers, abbreviations, or symbols are not pronounced as expected, or when synthesis quality for secondary languages is poor. Examples:
  • A number isn't read as expected: "hello, this is 110" is read as "hello, this is one one zero" instead of "hello, this is yao-yao-ling"
  • A symbol isn't read correctly: "@" is read as "ai-te" instead of "at"
  • Minor language synthesis sounds unnatural
  • zh: Chinese
  • en: English
  • fr: French
  • de: German
  • ja: Japanese
  • ko: Korean
  • ru: Russian
  • pt: Portuguese
  • th: Thai
  • id: Indonesian
  • vi: Vietnamese
  • es: Spanish
  • it: Italian
  • ms: Malaysian
  • fil: Filipino
  • ar: Arabic
Set language_hints using the HttpSpeechSynthesisParam instance's parameter method or parameters method: See Example 7 below See Example 8 below
instructionStringNoAn instruction that controls the synthesis behavior, such as dialect, emotion, or role. For usage details, see Non-real-time speech synthesis. Set instruction using the HttpSpeechSynthesisParam instance's parameter method or parameters method: See Example 9 below See Example 10 below
bit_rateintNoThe audio bitrate in kbps. Default value: 32. Valid values: [6, 510]. Tip: This parameter is supported only when format is set to opus. Set bit_rate using the HttpSpeechSynthesisParam instance's parameter method or parameters method: See Example 11 below See Example 12 below
enable_aigc_tagbooleanNoSpecifies whether to embed an AIGC watermark in the generated audio. When set to true, the watermark is embedded in audio files of supported formats (wav/mp3/opus). Default value: false. Note: Only cosyvoice-v3-flash, cosyvoice-v3-plus, cosyvoice-v2 support this feature. Set enable_aigc_tag using the HttpSpeechSynthesisParam instance's parameter method or parameters method: See Example 13 below See Example 14 below
aigc_propagatorStringNoSets the ContentPropagator field in the AIGC watermark, identifying the content propagator. Takes effect only when enable_aigc_tag is true. Default value: QwenCloud account ID. Note: Only cosyvoice-v3-flash, cosyvoice-v3-plus, cosyvoice-v2 support this feature. Set aigc_propagator using the HttpSpeechSynthesisParam instance's parameter method or parameters method: See Example 15 below See Example 16 below
aigc_propagate_idStringNoSets the PropagateID field in the AIGC watermark, uniquely identifying a specific propagation action. Takes effect only when enable_aigc_tag is true. Default value: The request ID of the current speech synthesis request. Note: Only cosyvoice-v3-flash, cosyvoice-v3-plus, cosyvoice-v2 support this feature. Set aigc_propagate_id using the HttpSpeechSynthesisParam instance's parameter method or parameters method: See Example 17 below See Example 18 below
hot_fixMapNoText hot-fix configuration. Customizes the pronunciation of specified words or substitutes text before speech synthesis. Note: Not supported by cosyvoice-v2. Parameters:
  • pronunciation: Custom pronunciation. Provides pinyin for given words to correct cases where the default pronunciation is inaccurate.
  • replace: Text substitution. Replaces specified words with target text before speech synthesis; the substituted text is what gets synthesized.
Example: See Example 19 below Set hot_fix using the HttpSpeechSynthesisParam instance's parameter method or parameters method: See Example 20 below See Example 21 below
enable_markdown_filterbooleanNoNote: Only cloned voices of cosyvoice-v3-flash support this feature. Specifies whether to enable Markdown filtering. When enabled, the system automatically strips Markdown markup symbols from the input text before synthesis, preventing them from being read aloud. Default value: false. Valid values:
  • true: Enable Markdown filtering
  • false: Disable Markdown filtering
Set enable_markdown_filter using the HttpSpeechSynthesisParam instance's parameter method or parameters method: See Example 22 below See Example 23 below
Example 1 (Description):
Using parameter
HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("<speak>Hello</speak>")
    .voice("longanyang")
    .parameter("enable_ssml", true)
    .build();
Example 2 (Description):
Using parameters
HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("<speak>Hello</speak>")
    .voice("longanyang")
    .parameters(Collections.singletonMap("enable_ssml", true))
    .build();
Example 3 (Description):
Using parameter
HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("Behind my house is a big garden.")
    .voice("longanyang")
    .parameter("word_timestamp_enabled", true)
    .build();
Example 4 (Description):
Using parameters
HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("Behind my house is a big garden.")
    .voice("longanyang")
    .parameters(Collections.singletonMap("word_timestamp_enabled", true))
    .build();
Example 5 (Description):
Using parameter
HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("Behind my house is a big garden.")
    .voice("longanyang")
    .parameter("seed", 1234)
    .build();
Example 6 (Description):
Using parameters
HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("Behind my house is a big garden.")
    .voice("longanyang")
    .parameters(Collections.singletonMap("seed", 1234))
    .build();
Example 7 (Description):
Using parameter
HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("Behind my house is a big garden.")
    .voice("longanyang")
    .parameter("language_hints", Arrays.asList("zh"))
    .build();
Example 8 (Description):
Using parameters
HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("Behind my house is a big garden.")
    .voice("longanyang")
    .parameters(Collections.singletonMap("language_hints", Arrays.asList("zh")))
    .build();
Example 9 (Description):
Using parameter
HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("Behind my house is a big garden.")
    .voice("longanyang")
    .parameter("instruction", "\u4f60\u8bf4\u8bdd\u7684\u60c5\u611f\u662fhappy\u3002") // longanyang requires this fixed Chinese instruction ("Your emotion is happy."), Unicode-escaped here.
    .build();
Example 10 (Description):
Using parameters
HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("Behind my house is a big garden.")
    .voice("longanyang")
    .parameters(Collections.singletonMap("instruction", "\u4f60\u8bf4\u8bdd\u7684\u60c5\u611f\u662fhappy\u3002")) // longanyang requires this fixed Chinese instruction ("Your emotion is happy."), Unicode-escaped here.
    .build();
Example 11 (Description):
Using parameter
HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("Behind my house is a big garden.")
    .voice("longanyang")
    .format("opus")
    .parameter("bit_rate", 32)
    .build();
Example 12 (Description):
Using parameters
HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("Behind my house is a big garden.")
    .voice("longanyang")
    .format("opus")
    .parameters(Collections.singletonMap("bit_rate", 32))
    .build();
Example 13 (Description):
Using parameter
HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("Behind my house is a big garden.")
    .voice("longanyang")
    .parameter("enable_aigc_tag", true)
    .build();
Example 14 (Description):
Using parameters
HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("Behind my house is a big garden.")
    .voice("longanyang")
    .parameters(Collections.singletonMap("enable_aigc_tag", true))
    .build();
Example 15 (Description):
Using parameter
HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("Behind my house is a big garden.")
    .voice("longanyang")
    .parameter("enable_aigc_tag", true)
    .parameter("aigc_propagator", "xxxx")
    .build();
Example 16 (Description):
Using parameters
Map<String, Object> map = new HashMap<>();
map.put("enable_aigc_tag", true);
map.put("aigc_propagator", "xxxx");

HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("Behind my house is a big garden.")
    .voice("longanyang")
    .parameters(map)
    .build();
Example 17 (Description):
Using parameter
HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("Behind my house is a big garden.")
    .voice("longanyang")
    .parameter("enable_aigc_tag", true)
    .parameter("aigc_propagate_id", "xxxx")
    .build();
Example 18 (Description):
Using parameters
Map<String, Object> map = new HashMap<>();
map.put("enable_aigc_tag", true);
map.put("aigc_propagate_id", "xxxx");

HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("Behind my house is a big garden.")
    .voice("longanyang")
    .parameters(map)
    .build();
Example 19 (Description):
"hot_fix": {
  "pronunciation": [
    {"weather": "tian1 qi4"}
  ],
  "replace": [
    {"today": "gold day"}
  ]
}
Example 20 (Description):
Using parameter
Map<String, Object> hotFix = new HashMap<>();

List<Map<String, String>> pronunciation = new ArrayList<>();
Map<String, String> pronItem = new HashMap<>();
pronItem.put("weather", "tian1 qi4");
pronunciation.add(pronItem);
hotFix.put("pronunciation", pronunciation);

List<Map<String, String>> replace = new ArrayList<>();
Map<String, String> replaceItem = new HashMap<>();
replaceItem.put("today", "gold day");
replace.add(replaceItem);
hotFix.put("replace", replace);

HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("The weather is really nice today.")
    .voice("longanyang")
    .parameter("hot_fix", hotFix)
    .build();
Example 21 (Description):
Using parameters
// Build the hotFix object as shown above
HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("The weather is really nice today.")
    .voice("longanyang")
    .parameters(Collections.singletonMap("hot_fix", hotFix))
    .build();
Example 22 (Description):
Using parameter
HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("# Title\nBody content")
    .voice("longanyang")
    .parameter("enable_markdown_filter", true)
    .build();
Example 23 (Description):
Using parameters
HttpSpeechSynthesisParam param = HttpSpeechSynthesisParam.builder()
    .model("cosyvoice-v3-plus")
    .text("# Title\nBody content")
    .voice("longanyang")
    .parameters(Collections.singletonMap("enable_markdown_filter", true))
    .build();

Code examples

The following examples show non-streaming and streaming calls using the CosyVoice speech synthesis API. Before running the examples, set the DASHSCOPE_API_KEY environment variable.
Each model version requires a matching voice. For example, cosyvoice-v3-flash and cosyvoice-v3-plus use voices such as longanyang, while cosyvoice-v2 uses voices such as longxiaochun_v2. When switching models, switch to a compatible voice. Each voice also supports specific languages. When synthesizing non-Chinese text, select a voice that supports the target language. For the complete model-to-voice mapping, see CosyVoice Voice list.
  • Non-streaming
  • Streaming
Non-streaming calls wait for synthesis to complete, then return the full result. Two return types are available:
  • callAndReturnAudio(): Returns audio binary data (ByteBuffer). Use this to save or process the audio directly.
  • call(): Returns an audio URL. Use this to download the audio from a URL.
import com.alibaba.dashscope.audio.http_tts.AudioInfo;
import com.alibaba.dashscope.audio.http_tts.HttpSpeechSynthesisParam;
import com.alibaba.dashscope.audio.http_tts.HttpSpeechSynthesisResult;
import com.alibaba.dashscope.audio.http_tts.HttpSpeechSynthesizer;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

public class CosyVoiceSyncExample {
    static {
        // China North 2 (Beijing). This model is available only in this region.
        // API keys are region-specific. See https://docs.qwencloud.com/developer-guides/getting-started/regions
        Constants.baseHttpApiUrl = "https://cn-beijing.maas.qwencloudapi.com/api/v1";
    }

    /**
     * Non-streaming example 1: Returns audio data (ByteBuffer)
     * Calls callAndReturnAudio, which blocks until synthesis completes and returns the full audio data.
     */
    public static void syncCallReturnAudio() {
        HttpSpeechSynthesizer synthesizer = new HttpSpeechSynthesizer();

        HttpSpeechSynthesisParam param =
            HttpSpeechSynthesisParam.builder()
                .model("cosyvoice-v3-plus")  // When switching models, also switch to a compatible voice version
                .text("Behind my house is a big garden.")
                .voice("longanyang")
                .format("wav")
                .sampleRate(24000)
                // If the environment variable is not set, replace with: apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                // Set additional parameters using the parameter method
                // .parameter("seed", 1234)
                // .parameter("enable_ssml", true)
                .build();

        try {
            ByteBuffer audioData = synthesizer.callAndReturnAudio(param);
            if (audioData != null && audioData.hasRemaining()) {
                byte[] bytes = new byte[audioData.remaining()];
                audioData.get(bytes);

                try (FileOutputStream fos = new FileOutputStream("sync_output.wav")) {
                    fos.write(bytes);
                    System.out.println("Audio saved to sync_output.wav, size: " + bytes.length + " bytes");
                } catch (IOException e) {
                    System.err.println("Failed to save audio: " + e.getMessage());
                }
            }
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            System.err.println("Synthesis failed: " + e.getMessage());
        }
        System.exit(0);
    }

    /**
     * Non-streaming example 2: Returns an audio URL
     * Calls call, which returns a result object containing an audio URL for downloading.
     */
    public static void syncCallReturnUrl() {
        HttpSpeechSynthesizer synthesizer = new HttpSpeechSynthesizer();

        HttpSpeechSynthesisParam param =
            HttpSpeechSynthesisParam.builder()
                .model("cosyvoice-v3-plus")  // When switching models, also switch to a compatible voice version
                .text("Behind my house is a big garden.")
                .voice("longanyang")
                .format("wav")
                .sampleRate(24000)
                // If the environment variable is not set, replace with: apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                .build();

        try {
            HttpSpeechSynthesisResult result = synthesizer.call(param);
            System.out.println("Request ID: " + result.getRequestId());

            if (result.hasAudioUrl()) {
                AudioInfo audioInfo = result.getAudioInfo();
                System.out.println("Audio URL: " + audioInfo.getUrl());
                System.out.println("Expires At: " + audioInfo.getExpiresAt());
                System.out.println("Remaining Time: " + audioInfo.getRemainingSeconds() + " seconds");
            }
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            System.err.println("Synthesis failed: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        // Non-streaming example 1: Returns audio data (ByteBuffer)
        // syncCallReturnAudio();
        // Non-streaming example 2: Returns an audio URL
        syncCallReturnUrl();
    }
}
CosyVoice HTTP Java SDK - QwenCloud