Skip to main content

Qwen-Audio-ASR / Fun-ASR Java SDK

Prerequisites

Quick start

Use the same examples for the models below. Replace the model ID using this table.
ModelModel ID in the example
Qwen-Audio-ASR-StreamingNon-streaming and callback (local file) — qwen-audio-3.1-asr-flash-streaming
Callback (microphone) and Flowable — qwen-audio-3.0-asr-flash-streaming
Qwen-Audio-ASR-Messageqwen-audio-3.1-asr-flash-message
Fun-ASR-Realtimefun-asr-realtime
The Recognition class provides interfaces for both synchronous calls and bidirectional streaming calls. Choose the approach that fits your needs:
  • Synchronous call: recognizes a local file and returns the complete result at once. Best for processing pre-recorded audio.
  • Bidirectional streaming call: recognizes an audio stream directly and returns results in real time. The audio stream can come from an external device, such as a microphone, or be read from a local file. Best for scenarios that require immediate feedback.
  • Synchronous call
  • Bidirectional streaming call: callback-based
  • Bidirectional streaming call: Flowable-based
Submit a single real-time speech recognition task and get the recognition result synchronously by passing in a local file. The call blocks until the result is returned.Instantiate The Recognition class, and call the call method to bind Request parameters and the file to recognize. The method performs recognition and returns the final result.
import com.alibaba.dashscope.audio.asr.recognition.Recognition;
import com.alibaba.dashscope.audio.asr.recognition.RecognitionParam;
import com.alibaba.dashscope.utils.Constants;

import java.io.File;

public class Main {
    public static void main(String[] args) {
        // The following is the configuration for the Singapore region. When calling, replace "{WorkspaceId}" with your real workspace ID. Configurations differ by region.
        Constants.baseWebsocketApiUrl = "wss://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api-ws/v1/inference";
        // Create a Recognition instance
        Recognition recognizer = new Recognition();
        // Create RecognitionParam
        RecognitionParam param =
                RecognitionParam.builder()
                        .model("qwen-audio-3.1-asr-flash-streaming")
                        // The API Key differs between the Singapore and Beijing regions. Get an API Key: https://www.alibabacloud.com/help/model-studio/get-api-key
                        // If you have not configured the environment variable, replace the following line with your Model Studio API Key: .apiKey("sk-xxx")
                        .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                        .format("wav")
                        .sampleRate(16000)
                        .build();

        try {
            System.out.println("Recognition result: " + recognizer.call(param, new File("{YOUR_AUDIO_FILE}")));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // Close the WebSocket connection after the task is complete
            recognizer.getDuplexApi().close(1000, "bye");
        }
        System.out.println(
                "[Metric] requestId: "
                        + recognizer.getLastRequestId()
                        + ", first package delay ms: "
                        + recognizer.getFirstPackageDelay()
                        + ", last package delay ms: "
                        + recognizer.getLastPackageDelay());
        System.exit(0);
    }
}

High-concurrency calls

The DashScope Java SDK uses the connection pooling of OkHttp3 to reduce the overhead of repeatedly establishing connections. For details, see High-concurrency best practices.

Request parameters

Qwen-Audio-ASR-Streaming

Shared reference Use the chained methods of RecognitionParam to configure the model, sample rate, audio format, and other parameters. Pass the configured parameter object to the call/streamCall method of The Recognition class.
RecognitionParam param = RecognitionParam.builder()
  .model("qwen-audio-3.0-asr-flash-streaming")
  .format("pcm")
  .sampleRate(16000)
  //.parameter("language_hints", new String[]{"zh"})
  .build();
ParameterTypeRequiredDescription
modelStringYesThe model name.
sampleRateIntegerYesThe sample rate in Hz. Any sample rate is supported.
formatStringYesThe audio format.Valid values:
  • pcm
  • wav
  • mp3
  • opus
  • speex
  • aac
  • amr
opus/speex: Must use Ogg encapsulation.wav: Must use PCM encoding.amr: Only the AMR-NB type is supported.
keep_dialectbooleanNoSupported by qwen-audio-3.1-asr-flash-streaming only. Defaults to false, which transcribes dialects into standard Chinese. Set to true to preserve dialect expressions. Set with .parameter("keep_dialect", value). See Client events for details.
vad_modelStringNoSupported by qwen-audio-3.1-asr-flash-streaming only. Set to near_meeting_16k (near-field) or far_field_meeting_16k (far-field, default). Set with .parameter("vad_model", value). See Client events for details.
vocabularyIdStringNoThe ID of a precompiled hot word list.Generate this ID in advance by calling the create hot word list API. Pass the ID during recognition to use the hot words in the list.Suitable for scenarios where the vocabulary is known and relatively stable, and where you need to reuse the same word list across requests.For usage details, see Precompiled hotwords.
vocabularyMap<String, Integer>NoInstant hot words.Passed as key-value pairs, where the key is the hot word text (string) and the value is the hot word weight (integer). No hot word list needs to be created in advance. The weight ranges from [1, 5] or is set to 50: a value in [1, 5] makes the model more likely to output the word as the value increases; a value of 50 designates a super hot word, which greatly improves recall, but the number of super hot words cannot exceed 50.Suitable for temporary, session-level hot word optimization.When instant and precompiled hotwords are configured together, the system merges both sets. If the merged set contains more than 2000 hotwords, the system randomly selects 2000 to use. For usage details, see Instant hotwords.Set vocabulary through the parameter method or the parameters method of the RecognitionParam instance:
Map<String, Integer> vocab = new HashMap<>();
vocab.put("John Smith", 5);
vocab.put("Jane Doe", 5);

RecognitionParam param = RecognitionParam.builder()
        .model("qwen-audio-3.1-asr-flash-streaming")
        .format("pcm")
        .sampleRate(16000)
        .parameter("vocabulary", vocab)
        .build();
max_sentence_silenceIntegerNoThe VAD silence threshold for segmentation, in ms. When the silence after a segment of speech exceeds this threshold, the system determines that the sentence has ended.Default value: 800.Valid values: [200, 6000].Set max_sentence_silence through the parameter method or the parameters method of the RecognitionParam instance:
RecognitionParam param = RecognitionParam.builder()
 .model("qwen-audio-3.1-asr-flash-streaming")
 .format("pcm")
 .sampleRate(16000)
 .parameter("max_sentence_silence", 800)
 .build();
multi_threshold_mode_enabledbooleanNoWhether to enable multi-threshold mode. When enabled, this prevents VAD segments from becoming too long.Default value: false.Set multi_threshold_mode_enabled through the parameter method or the parameters method of the RecognitionParam instance:
RecognitionParam param = RecognitionParam.builder()
 .model("qwen-audio-3.1-asr-flash-streaming")
 .format("pcm")
 .sampleRate(16000)
 .parameter("multi_threshold_mode_enabled", true)
 .build();
punctuation_prediction_enabledbooleanNoSets whether to automatically add punctuation to the recognition results:
  • true (default): yes. This value cannot be changed.
Set punctuation_prediction_enabled through the parameter method or the parameters method of the RecognitionParam instance:
RecognitionParam param = RecognitionParam.builder()
 .model("qwen-audio-3.1-asr-flash-streaming")
 .format("pcm")
 .sampleRate(16000)
 .parameter("punctuation_prediction_enabled", false)
 .build();
heartbeatbooleanNoWhether to enable heartbeat packets.Default value: false.
  • true: Keeps the connection to the server alive even when silent audio is sent continuously.
  • false (default): Even when silent audio is continuously sent, the connection times out and closes after a period of time.
Silent audio refers to content in an audio file or data stream that contains no sound signal. You can generate silent audio in several ways, such as using audio editing software like Audacity or Adobe Audition, or using a command-line tool like FFmpeg.
To use this field, the SDK version must be 2.19.1 or later.
Set heartbeat through the parameter method or the parameters method of the RecognitionParam instance:
RecognitionParam param = RecognitionParam.builder()
 .model("qwen-audio-3.1-asr-flash-streaming")
 .format("pcm")
 .sampleRate(16000)
 .parameter("heartbeat", true)
 .build();
language_hintsString[]NoThe language of the audio to recognize. There is no default value; if not set, the model detects the language automatically.You can set up to 4 values. If you set more, only the first 4 take effect.
  • zh: Chinese
  • en: English
  • ja: Japanese
  • ko: Korean
  • vi: Vietnamese
  • th: Thai
  • id: Indonesian
  • ms: Malay
  • tl: Filipino
  • hi: Hindi
  • ar: Arabic
  • fr: French
  • de: German
  • es: Spanish
  • pt: Portuguese
  • ru: Russian
  • it: Italian
  • nl: Dutch
  • sv: Swedish
  • da: Danish
  • fi: Finnish
  • no: Norwegian
  • el: Greek
  • pl: Polish
  • cs: Czech
  • hu: Hungarian
  • ro: Romanian
  • bg: Bulgarian
  • hr: Croatian
  • sk: Slovak
Set language_hints through the parameter method or the parameters method of the RecognitionParam instance:
RecognitionParam param = RecognitionParam.builder()
 .model("qwen-audio-3.1-asr-flash-streaming")
 .format("pcm")
 .sampleRate(16000)
 .parameter("language_hints", new String[]{"zh"})
 .build();
speech_noise_thresholdfloatNoThe threshold for distinguishing speech from noise, used to adjust the sensitivity of Voice Activity Detection (VAD).Valid values: [-1.0, 1.0].Value descriptions:
  • The closer the value is to -1: The noise threshold decreases, so noise is more likely to be recognized as speech, which may cause more noise to be transcribed.
  • The closer the value is to +1: The noise threshold increases, so speech is more likely to be misjudged as noise, which may cause some speech to be filtered out.
This is an advanced configuration parameter. Adjusting it can significantly affect recognition results. Recommendations:
  • Thoroughly test and verify the results before adjusting.
  • Adjust in small increments based on the actual audio environment (a step of 0.1 is recommended).
Set speech_noise_threshold through the parameter method or the parameters method of the RecognitionParam instance:
RecognitionParam param = RecognitionParam.builder()
 .model("qwen-audio-3.1-asr-flash-streaming")
 .format("pcm")
 .sampleRate(16000)
 .parameter("speech_noise_threshold", -0.5)
 .build();
special_word_filterStringNoSpecifies the sensitive words to process during speech recognition, and supports setting different processing methods for different sensitive words. For details, see Sensitive word filtering.Set special_word_filter through the parameter method or the parameters method of the RecognitionParam instance:
// 1. Build the outermost object
JSONObject root = new JSONObject();
root.put("system_reserved_filter", true);

// 2. Build the "remove completely from results" configuration
JSONObject root1 = new JSONObject();
JSONArray array1 = new JSONArray();
array1.put("start");
array1.put("proceed");
root1.put("word_list", array1);

// 3. Build the "replace with equal-length *" configuration
JSONObject root2 = new JSONObject();
JSONArray array2 = new JSONArray();
array2.put("test");
root2.put("word_list", array2);

// 4. Assemble
root.put("filter_with_empty", root1);
root.put("filter_with_signed", root2);

RecognitionParam param = RecognitionParam.builder()
 .model("qwen-audio-3.1-asr-flash-streaming")
 .format("pcm")
 .sampleRate(16000)
 .parameter("special_word_filter", root.toString())
 .build();
inputMap<String, Object>NoInput object that passes in the conversation context. The context helps recognition and improves the recognition accuracy of proper terms. For usage, see Quick start.The Map must contain a context key whose value is a message array of type List<Map<String, Object>>. Each message contains the following fields:
  • role (String, required): the message role. user indicates the recognition result of the user's speech from previous rounds or a domain-specific word list. assistant indicates the large language model's replies from previous rounds.
  • content (List<Map>, required): the message content list. Each element contains type (String; set to input_text when role is user, and text when role is assistant) and text (String, the text content).
Limits: context messages of the input_text and text types are limited to 5 each; the most recent 5 are kept when the limit is exceeded. The total text length per round of context cannot exceed 400 characters, and any excess is truncated from the end.
When you pass in context, the messages in context must follow a specific order: context messages must be arranged by conversation round, and within each round the user message (input_text type) must precede the corresponding assistant message (text type).
To use this field, the SDK version must be 2.22.23 or later.
Set input through the input method of the RecognitionParam instance:
// 1. Build the input struct
      Map<String, Object> userContent = new HashMap<>();
      userContent.put("type", "input_text");
      userContent.put("text", "Hello there");

      Map<String, Object> assistantContent = new HashMap<>();
      assistantContent.put("type", "text");
      assistantContent.put("text", "Hello, I am Qwen. How can I help you?");

      Map<String, Object> userMessage = new HashMap<>();
      userMessage.put("role", "user");
      userMessage.put("content", Arrays.asList(userContent));

      Map<String, Object> assistantMessage = new HashMap<>();
      assistantMessage.put("role", "assistant");
      assistantMessage.put("content", Arrays.asList(assistantContent));

      Map<String, Object> input = new HashMap<>();
      input.put("context", Arrays.asList(userMessage, assistantMessage));

      // 2. Pass it in through the input method
      RecognitionParam param = RecognitionParam.builder()
       .model("qwen-audio-3.1-asr-flash-streaming")
       .format("pcm")
       .sampleRate(16000)
       .input(input)
       .build();
apiKeyStringNoYour API key.

Qwen-Audio-ASR-Message

Shared reference
Alibaba Cloud Model Studio has released workspace-specific domains for the China (Beijing) and Singapore regions. The new dedicated domains deliver superior performance and higher stability for inference requests. We recommend migrating to the new domains:
  • China (Beijing): from dashscope.aliyuncs.com to {WorkspaceId}.cn-beijing.maas.aliyuncs.com
  • Singapore: from dashscope-intl.aliyuncs.com to {WorkspaceId}.ap-southeast-1.maas.aliyuncs.com
Use the chained methods of RecognitionParam to configure the model, sample rate, audio format, and other parameters. Pass the configured parameter object to the call/streamCall method of The Recognition class.
RecognitionParam param = RecognitionParam.builder()
  .model("qwen-audio-3.1-asr-flash-message")
  .format("pcm")
  .sampleRate(16000)
  .build();
ParameterTypeRequiredDescription
modelStringYesThe model name. Set to qwen-audio-3.1-asr-flash-message.
sampleRateIntegerYes

The sample rate, in Hz.

Only 16000 Hz is supported.
formatStringYes

The audio format.

Valid values:
  • pcm
  • wav
  • mp3
  • opus
  • speex
  • aac
  • amr
opus/speex: Must use Ogg encapsulation.wav: Must use PCM encoding.amr: Only the AMR-NB type is supported.
disfluency_removal_enabledbooleanNoWhether to filter filler words and polish the output. Defaults to false. Set to true to enable this feature. Set with .parameter("disfluency_removal_enabled", value).
intermediate_result_enabledbooleanNoWhether to return intermediate streaming results. Defaults to false. Set to true to return intermediate streaming results. Set with .parameter("intermediate_result_enabled", value).
keep_dialectbooleanNoDefaults to false, which transcribes dialects into standard Chinese. Set to true to preserve dialect expressions. Set with .parameter("keep_dialect", value). See Client events for details.This parameter applies only to final results (sentence_end=true) and is not supported for intermediate results (sentence_end=false).
vad_modelStringNoSet to near_meeting_16k (near-field) or far_field_meeting_16k (far-field, default). Set with .parameter("vad_model", value). See Client events for details.
vocabularyIdStringNo

The ID of a precompiled hot word list.

Generate this ID in advance by calling the create hot word list API. Pass the ID during recognition to use the hot words in the list.Suitable for scenarios where the vocabulary is known and relatively stable, and where you need to reuse the same word list across requests.For usage details, see Precompiled hotwords.
vocabularyMap<String, Integer>No

Instant hot words.

Passed as key-value pairs, where the key is the hot word text (string) and the value is the hot word weight (integer). No hot word list needs to be created in advance. The weight ranges from [1, 5] or is set to 50: a value in [1, 5] makes the model more likely to output the word as the value increases; a value of 50 designates a super hot word, which greatly improves recall, but the number of super hot words cannot exceed 50.Suitable for temporary, session-level hot word optimization.When instant and precompiled hotwords are configured together, the system merges both sets. If the merged set contains more than 2000 hotwords, the system randomly selects 2000 to use. For usage details, see Instant hotwords.Set vocabulary through the parameter method or the parameters method of the RecognitionParam instance:
Map<String, Integer> vocab = new HashMap<>();
vocab.put("John Smith", 5);
vocab.put("Jane Doe", 5);

RecognitionParam param = RecognitionParam.builder()
        .model("qwen-audio-3.1-asr-flash-message")
        .format("pcm")
        .sampleRate(16000)
        .parameter("vocabulary", vocab)
        .build();
language_hintsString[]NoThe language of the audio to recognize. There is no default value; if not set, the model detects the language automatically.You can set up to 4 values. If you set more, only the first 4 take effect.
  • zh: Chinese
  • en: English
  • ja: Japanese
  • ko: Korean
  • vi: Vietnamese
  • th: Thai
  • id: Indonesian
  • ms: Malay
  • tl: Filipino
  • hi: Hindi
  • ar: Arabic
  • fr: French
  • de: German
  • es: Spanish
  • pt: Portuguese
  • ru: Russian
  • it: Italian
  • nl: Dutch
  • sv: Swedish
  • da: Danish
  • fi: Finnish
  • no: Norwegian
  • el: Greek
  • pl: Polish
  • cs: Czech
  • hu: Hungarian
  • ro: Romanian
  • bg: Bulgarian
  • hr: Croatian
  • sk: Slovak
Set this parameter through .parameter("language_hints", new String[]{"zh"}) on RecognitionParam.This parameter applies only to final results (sentence_end=true) and is not supported for intermediate results (sentence_end=false).
max_sentence_silenceIntegerNo

The VAD silence threshold for segmentation, in ms. When the silence after a segment of speech exceeds this threshold, the system determines that the sentence has ended.

Default value: 800.Valid values: [200, 6000].Set max_sentence_silence through the parameter method or the parameters method of the RecognitionParam instance:
RecognitionParam param = RecognitionParam.builder()
 .model("qwen-audio-3.1-asr-flash-message")
 .format("pcm")
 .sampleRate(16000)
 .parameter("max_sentence_silence", 800)
 .build();
heartbeatbooleanNo

Whether to enable heartbeat packets.

Default value: false.
  • true: Keeps the connection to the server alive even when silent audio is sent continuously.
  • false (default): Even when silent audio is continuously sent, the connection times out and closes after a period of time.
Silent audio refers to content in an audio file or data stream that contains no sound signal. You can generate silent audio in several ways, such as using audio editing software like Audacity or Adobe Audition, or using a command-line tool like FFmpeg.
To use this field, the SDK version must be 2.19.1 or later.
Set heartbeat through the parameter method or the parameters method of the RecognitionParam instance:
RecognitionParam param = RecognitionParam.builder()
 .model("qwen-audio-3.1-asr-flash-message")
 .format("pcm")
 .sampleRate(16000)
 .parameter("heartbeat", true)
 .build();
speech_noise_thresholdfloatNo

The threshold for distinguishing speech from noise, used to adjust the sensitivity of Voice Activity Detection (VAD).

Valid values: [-1.0, 1.0].Value descriptions:
  • The closer the value is to -1: The noise threshold decreases, so noise is more likely to be recognized as speech, which may cause more noise to be transcribed.
  • The closer the value is to +1: The noise threshold increases, so speech is more likely to be misjudged as noise, which may cause some speech to be filtered out.
This is an advanced configuration parameter. Adjusting it can significantly affect recognition results. Recommendations:
  • Thoroughly test and verify the results before adjusting.
  • Adjust in small increments based on the actual audio environment (a step of 0.1 is recommended).
Set speech_noise_threshold through the parameter method or the parameters method of the RecognitionParam instance:
RecognitionParam param = RecognitionParam.builder()
 .model("qwen-audio-3.1-asr-flash-message")
 .format("pcm")
 .sampleRate(16000)
 .parameter("speech_noise_threshold", -0.5)
 .build();
inputMap<String, Object>No

Input object that passes in the conversation context. The context helps recognition and improves the recognition accuracy of proper terms. For usage, see Quick start.

The Map must contain a context key whose value is a message array of type List<Map<String, Object>>. Each message contains the following fields:
  • role (String, required): the message role. user indicates the recognition result of the user's speech from previous rounds or a domain-specific word list. assistant indicates the large language model's replies from previous rounds.
  • content (List<Map>, required): the message content list. Each element contains type (String; set to input_text when role is user, and text when role is assistant) and text (String, the text content).
Limits: context messages of the input_text and text types are limited to 5 each; the most recent 5 are kept when the limit is exceeded. The total text length per round of context cannot exceed 400 characters, and any excess is truncated from the end.
When you pass in context, the messages in context must follow a specific order: context messages must be arranged by conversation round, and within each round the user message (input_text type) must precede the corresponding assistant message (text type).
To use this field, the SDK version must be 2.22.23 or later.
Set input through the input method of the RecognitionParam instance:
// 1. Build the input struct
      Map<String, Object> userContent = new HashMap<>();
      userContent.put("type", "input_text");
      userContent.put("text", "Hello there");

      Map<String, Object> assistantContent = new HashMap<>();
      assistantContent.put("type", "text");
      assistantContent.put("text", "Hello, I am Qwen. How can I help you?");

      Map<String, Object> userMessage = new HashMap<>();
      userMessage.put("role", "user");
      userMessage.put("content", Arrays.asList(userContent));

      Map<String, Object> assistantMessage = new HashMap<>();
      assistantMessage.put("role", "assistant");
      assistantMessage.put("content", Arrays.asList(assistantContent));

      Map<String, Object> input = new HashMap<>();
      input.put("context", Arrays.asList(userMessage, assistantMessage));

      // 2. Pass it in through the input method
      RecognitionParam param = RecognitionParam.builder()
       .model("qwen-audio-3.1-asr-flash-message")
       .format("pcm")
       .sampleRate(16000)
       .input(input)
       .build();
apiKeyStringNoYour API key.

Fun-ASR-Realtime

Shared reference Use the chained methods of RecognitionParam to configure the model, sample rate, audio format, and other parameters. Pass the configured parameter object to the call/streamCall method of The Recognition class.
RecognitionParam param = RecognitionParam.builder()
  .model("fun-asr-realtime")
  .format("pcm")
  .sampleRate(16000)
  //.parameter("language_hints", new String[]{"zh"})
  .build();
ParameterTypeRequiredDescription
modelStringYesThe model name.
sampleRateIntegerYesThe sample rate, in Hz.Valid values: 8 kHz models support only 8000 Hz; other models support any sample rate.
formatStringYesThe audio format.Valid values:
  • pcm
  • wav
  • mp3
  • opus
  • speex
  • aac
  • amr
opus/speex: Must use Ogg encapsulation.wav: Must use PCM encoding.amr: Only the AMR-NB type is supported.
vocabularyIdStringNoThe ID of a precompiled hot word list.Generate this ID in advance by calling the create hot word list API. Pass the ID during recognition to use the hot words in the list.Suitable for scenarios where the vocabulary is known and relatively stable, and where you need to reuse the same word list across requests.For usage details, see Precompiled hotwords.
max_sentence_silenceIntegerNoThe VAD silence threshold for segmentation, in ms. When the silence after a segment of speech exceeds this threshold, the system determines that the sentence has ended.Default value: 1300.Valid values: [200, 6000].Set max_sentence_silence through the parameter method or the parameters method of the RecognitionParam instance:
RecognitionParam param = RecognitionParam.builder()
 .model("fun-asr-realtime")
 .format("pcm")
 .sampleRate(16000)
 .parameter("max_sentence_silence", 800)
 .build();
multi_threshold_mode_enabledbooleanNoWhether to enable multi-threshold mode. When enabled, this prevents VAD segments from becoming too long.Default value: false.Set multi_threshold_mode_enabled through the parameter method or the parameters method of the RecognitionParam instance:
RecognitionParam param = RecognitionParam.builder()
 .model("fun-asr-realtime")
 .format("pcm")
 .sampleRate(16000)
 .parameter("multi_threshold_mode_enabled", true)
 .build();
punctuation_prediction_enabledbooleanNoSets whether to automatically add punctuation to the recognition results:
  • true (default): yes. This value cannot be changed.
Set punctuation_prediction_enabled through the parameter method or the parameters method of the RecognitionParam instance:
RecognitionParam param = RecognitionParam.builder()
 .model("fun-asr-realtime")
 .format("pcm")
 .sampleRate(16000)
 .parameter("punctuation_prediction_enabled", false)
 .build();
heartbeatbooleanNoWhether to enable heartbeat packets.Default value: false.
  • true: Keeps the connection to the server alive even when silent audio is sent continuously.
  • false (default): Even when silent audio is continuously sent, the connection times out and closes after a period of time.
Silent audio refers to content in an audio file or data stream that contains no sound signal. You can generate silent audio in several ways, such as using audio editing software like Audacity or Adobe Audition, or using a command-line tool like FFmpeg.
To use this field, the SDK version must be 2.19.1 or later.
Set heartbeat through the parameter method or the parameters method of the RecognitionParam instance:
RecognitionParam param = RecognitionParam.builder()
 .model("fun-asr-realtime")
 .format("pcm")
 .sampleRate(16000)
 .parameter("heartbeat", true)
 .build();
language_hintsString[]NoThe language of the audio to recognize. There is no default value; if not set, the model detects the language automatically.You can set 1 value. If you set more, only the first value takes effect.
  • fun-asr-realtime:
    • zh: Chinese
    • en: English
    • ja: Japanese
    • ko: Korean
    • vi: Vietnamese
    • th: Thai
    • id: Indonesian
    • ms: Malay
    • tl: Filipino
    • hi: Hindi
    • ar: Arabic
    • fr: French
    • de: German
    • es: Spanish
    • pt: Portuguese
    • ru: Russian
    • it: Italian
    • nl: Dutch
    • sv: Swedish
    • da: Danish
    • fi: Finnish
    • no: Norwegian
    • el: Greek
    • pl: Polish
    • cs: Czech
    • hu: Hungarian
    • ro: Romanian
    • bg: Bulgarian
    • hr: Croatian
    • sk: Slovak
  • fun-asr-realtime-2026-02-28:
    • zh: Chinese
    • en: English
    • ja: Japanese
  • fun-asr-flash-8k-realtime, fun-asr-flash-8k-realtime-2026-01-28:
    • zh: Chinese
Set language_hints through the parameter method or the parameters method of the RecognitionParam instance:
RecognitionParam param = RecognitionParam.builder()
 .model("fun-asr-realtime")
 .format("pcm")
 .sampleRate(16000)
 .parameter("language_hints", new String[]{"zh"})
 .build();
speech_noise_thresholdfloatNoThe threshold for distinguishing speech from noise, used to adjust the sensitivity of Voice Activity Detection (VAD).Valid values: [-1.0, 1.0].Value descriptions:
  • The closer the value is to -1: The noise threshold decreases, so noise is more likely to be recognized as speech, which may cause more noise to be transcribed.
  • The closer the value is to +1: The noise threshold increases, so speech is more likely to be misjudged as noise, which may cause some speech to be filtered out.
This is an advanced configuration parameter. Adjusting it can significantly affect recognition results. Recommendations:
  • Thoroughly test and verify the results before adjusting.
  • Adjust in small increments based on the actual audio environment (a step of 0.1 is recommended).
Set speech_noise_threshold through the parameter method or the parameters method of the RecognitionParam instance:
RecognitionParam param = RecognitionParam.builder()
 .model("fun-asr-realtime")
 .format("pcm")
 .sampleRate(16000)
 .parameter("speech_noise_threshold", -0.5)
 .build();
special_word_filterStringNoSpecifies the sensitive words to process during speech recognition, and supports setting different processing methods for different sensitive words. For details, see Sensitive word filtering.Set special_word_filter through the parameter method or the parameters method of the RecognitionParam instance:
// 1. Build the outermost object
JSONObject root = new JSONObject();
root.put("system_reserved_filter", true);

// 2. Build the "remove completely from results" configuration
JSONObject root1 = new JSONObject();
JSONArray array1 = new JSONArray();
array1.put("start");
array1.put("proceed");
root1.put("word_list", array1);

// 3. Build the "replace with equal-length *" configuration
JSONObject root2 = new JSONObject();
JSONArray array2 = new JSONArray();
array2.put("test");
root2.put("word_list", array2);

// 4. Assemble
root.put("filter_with_empty", root1);
root.put("filter_with_signed", root2);

RecognitionParam param = RecognitionParam.builder()
 .model("fun-asr-realtime")
 .format("pcm")
 .sampleRate(16000)
 .parameter("special_word_filter", root.toString())
 .build();
inputMap<String, Object>NoInput object that passes in the conversation context. The context helps recognition and improves the recognition accuracy of proper terms. For usage, see Quick start.
Only the fun-asr-realtime models support the context parameter.
The Map must contain a context key whose value is a message array of type List<Map<String, Object>>. Each message contains the following fields:
  • role (String, required): the message role. user indicates the recognition result of the user's speech from previous rounds or a domain-specific word list. assistant indicates the large language model's replies from previous rounds.
  • content (List<Map>, required): the message content list. Each element contains type (String; set to input_text when role is user, and text when role is assistant) and text (String, the text content).
Limits: context messages of the input_text and text types are limited to 5 each; the most recent 5 are kept when the limit is exceeded. The total text length per round of context cannot exceed 400 characters, and any excess is truncated from the end.
When you pass in context, the messages in context must follow a specific order: context messages must be arranged by conversation round, and within each round the user message (input_text type) must precede the corresponding assistant message (text type).
To use this field, the SDK version must be 2.22.23 or later.
Set input through the input method of the RecognitionParam instance:
// 1. Build the input struct
      Map<String, Object> userContent = new HashMap<>();
      userContent.put("type", "input_text");
      userContent.put("text", "Hello there");

      Map<String, Object> assistantContent = new HashMap<>();
      assistantContent.put("type", "text");
      assistantContent.put("text", "Hello, I am Qwen. How can I help you?");

      Map<String, Object> userMessage = new HashMap<>();
      userMessage.put("role", "user");
      userMessage.put("content", Arrays.asList(userContent));

      Map<String, Object> assistantMessage = new HashMap<>();
      assistantMessage.put("role", "assistant");
      assistantMessage.put("content", Arrays.asList(assistantContent));

      Map<String, Object> input = new HashMap<>();
      input.put("context", Arrays.asList(userMessage, assistantMessage));

      // 2. Pass it in through the input method
      RecognitionParam param = RecognitionParam.builder()
       .model("fun-asr-realtime")
       .format("pcm")
       .sampleRate(16000)
       .input(input)
       .build();
apiKeyStringNoYour API key.

API reference

Qwen-Audio-ASR-Streaming / Fun-ASR-Realtime

The Recognition class

Import Recognition with import com.alibaba.dashscope.audio.asr.recognition.Recognition;. Its key interfaces are as follows:
Interface/MethodParameterReturn valueDescription
public void call(RecognitionParam param, final ResultCallback<RecognitionResult> callback)
  • param: Request parameters
  • callback: The callback interface (ResultCallback)
NoneCallback-based streaming real-time recognition. This method does not block the current thread.
public String call(RecognitionParam param, File file)
  • param: Request parameters
  • file: The audio file to recognize.
The recognition result.Non-streaming recognition of a local file. This method blocks the current thread until the entire audio file is read. The file must be readable.
public Flowable<RecognitionResult> streamCall(RecognitionParam param, Flowable<ByteBuffer> audioFrame)
  • param: Request parameters
  • audioFrame: A Flowable<ByteBuffer> instance.
Flowable<RecognitionResult>Flowable-based streaming real-time recognition.
public void sendAudioFrame(ByteBuffer audioFrame)
  • audioFrame: A binary audio stream of type ByteBuffer.
NoneSends audio. Keep each pushed audio chunk within a reasonable size. A recommended chunk holds about 100 ms of audio and is 1 KB to 16 KB in size.Recognition results are delivered through the onEvent method of the The callback interface (ResultCallback).
public void stop()
NoneNoneStops real-time recognition.This method blocks the current thread until the ResultCallback callback's onComplete or onError is called.
boolean getDuplexApi().close(int code, String reason)
code: The WebSocket close code.reason: The reason for closing.For guidance on setting these two parameters, see The WebSocket Protocol.trueAfter a task ends, always close the WebSocket connection, whether or not an exception occurred, to avoid connection leaks. To reuse connections for better efficiency, see High-concurrency best practices.
public String getLastRequestId()
NonerequestIdGets the requestId of the current task. Available after a new task starts with call or streamingCall.
This method is available only in SDK version 2.18.0 and later.
public long getFirstPackageDelay()
NoneThe first-packet latency.Gets the first-packet latency, that is, the delay from sending the first audio packet to receiving the first recognition result. Use it after the task completes.
This method is available only in SDK version 2.18.0 and later.
public long getLastPackageDelay()
NoneThe last-packet latency.Gets the last-packet latency, that is, the time from sending the stop command to receiving the final recognition result. Use it after the task completes.
This method is available only in SDK version 2.18.0 and later.

The callback interface (ResultCallback)

During bidirectional streaming calls, the server returns key process information and data to the client through callbacks. Implement the callback methods to handle the information or data returned by the server. Implement the callback methods by extending the abstract class ResultCallback. When you extend this class, you can set the generic type to RecognitionResult. RecognitionResult wraps the data structure returned by the server. Because Java supports connection reuse, there is no onClose or onOpen.
ResultCallback<RecognitionResult> callback = new ResultCallback<RecognitionResult>() {
    @Override
    public void onEvent(RecognitionResult result) {
        System.out.println("RequestId: " + result.getRequestId());
        // Add your logic to process the speech recognition result here.
    }

    @Override
    public void onComplete() {
        System.out.println("Task complete");
    }

    @Override
    public void onError(Exception e) {
        System.out.println("Task failed: " + e.getMessage());
    }
};
Interface/MethodParameterReturn valueDescription
public void onEvent(RecognitionResult result)
result: Real-time recognition result (RecognitionResult)NoneCalled when the server sends a response.
public void onComplete()
NoneNoneCalled after the task completes.
public void onError(Exception e)
e: The exception information.NoneCalled when an exception occurs.

Real-time recognition result (RecognitionResult)

RecognitionResult represents the result of a single real-time recognition.
Interface/MethodParameterReturn valueDescription
public String getRequestId()
NonerequestIdGets the requestId.
public boolean isSentenceEnd()
NoneWhether a complete sentence has been formed, that is, whether a sentence boundary was detected.Determines whether the given sentence has ended.
public Sentence getSentence()
NoneSentence information (Sentence)Gets the sentence information, including timestamps and text.

Sentence information (Sentence)

Interface/MethodParameterReturn valueDescription
public Long getBeginTime()
NoneThe sentence start time, in ms.Returns the sentence start time.
public Long getEndTime()
NoneThe sentence end time, in ms.Returns the sentence end time.
public String getText()
NoneThe recognized text.Returns the recognized text.
public List<Word> getWords()
NoneA List of Word-level timestamp information (Word) objects.Returns word-level timestamp information.

Word-level timestamp information (Word)

Interface/MethodParameterReturn valueDescription
public long getBeginTime()
NoneThe word start time, in ms.Returns the word start time.
public long getEndTime()
NoneThe word end time, in ms.Returns the word end time.
public String getText()
NoneThe word.Returns the recognized word.
public String getPunctuation()
NoneThe punctuation.Returns the punctuation.

Qwen-Audio-ASR-Streaming

Key interfaces

Response

Qwen-Audio-ASR-Message

Key interfaces

The Recognition class
Import Recognition with import com.alibaba.dashscope.audio.asr.recognition.Recognition;. Its key interfaces are as follows:
Interface/MethodParameterReturn valueDescription
public void call(RecognitionParam param, final ResultCallback<RecognitionResult> callback)
NoneCallback-based streaming real-time recognition. This method does not block the current thread.
public String call(RecognitionParam param, File file)
The recognition result.Non-streaming recognition of a local file. This method blocks the current thread until the entire audio file is read. The file must be readable.
public Flowable<RecognitionResult> streamCall(RecognitionParam param, Flowable<ByteBuffer> audioFrame)
Flowable<RecognitionResult>Flowable-based streaming real-time recognition.
public void sendAudioFrame(ByteBuffer audioFrame)
  • audioFrame: A binary audio stream of type ByteBuffer.
None

Sends audio. Keep each pushed audio chunk within a reasonable size. A recommended chunk holds about 100 ms of audio and is 1 KB to 16 KB in size.

Recognition results are delivered through the onEvent method of the The callback interface (ResultCallback).
public void stop()
NoneNone

Stops real-time recognition.

This method blocks the current thread until the ResultCallback callback's onComplete or onError is called.
boolean getDuplexApi().close(int code, String reason)

code: The WebSocket close code.

reason: The reason for closing.For guidance on setting these two parameters, see The WebSocket Protocol.
trueAfter a task ends, always close the WebSocket connection, whether or not an exception occurred, to avoid connection leaks. To reuse connections for better efficiency, see High-concurrency best practices.
public String getLastRequestId()
NonerequestId

Gets the requestId of the current task. Available after a new task starts with call or streamingCall.

This method is available only in SDK version 2.18.0 and later.
public long getFirstPackageDelay()
NoneThe first-packet latency.

Gets the first-packet latency, that is, the delay from sending the first audio packet to receiving the first recognition result. Use it after the task completes.

This method is available only in SDK version 2.18.0 and later.
public long getLastPackageDelay()
NoneThe last-packet latency.

Gets the last-packet latency, that is, the time from sending the stop command to receiving the final recognition result. Use it after the task completes.

This method is available only in SDK version 2.18.0 and later.
Update conversation context
Call updateContext to update conversation context while a recognition task is running. The context is used to assist recognition of subsequent audio. This method requires DashScope Java SDK 2.23.0 or later.
public void updateContext(Map<String, Object> payloadInput)
  • When to call: After starting streaming recognition with call(param, callback) and before calling stop.
  • Parameter: payloadInput is a Map<String, Object> containing the payload.input object of a continue-task event, including the context field. Do not add an extra payload or input wrapper.
  • Supported models and parameter constraints: See continue-task.
The following example uses an existing, started recognizer instance. Import the Java collection types with import java.util.*;.
Map<String, Object> userContent = new HashMap<>();
userContent.put("type", "input_text");
userContent.put("text", "This is round 1 of the speech recognition test");
Map<String, Object> userMessage = new HashMap<>();
userMessage.put("role", "user");
userMessage.put("content", Collections.singletonList(userContent));

Map<String, Object> assistantContent = new HashMap<>();
assistantContent.put("type", "text");
assistantContent.put("text", "OK, please start round 1 of the test");
Map<String, Object> assistantMessage = new HashMap<>();
assistantMessage.put("role", "assistant");
assistantMessage.put("content", Collections.singletonList(assistantContent));

Map<String, Object> payloadInput = new HashMap<>();
payloadInput.put("context", Arrays.asList(userMessage, assistantMessage));
recognizer.updateContext(payloadInput);
The callback interface (ResultCallback)
During bidirectional streaming calls, the server returns key process information and data to the client through callbacks. Implement the callback methods to handle the information or data returned by the server. Implement the callback methods by extending the abstract class ResultCallback. When you extend this class, you can set the generic type to RecognitionResult. RecognitionResult wraps the data structure returned by the server. Because Java supports connection reuse, there is no onClose or onOpen.
ResultCallback<RecognitionResult> callback = new ResultCallback<RecognitionResult>() {
    @Override
    public void onEvent(RecognitionResult result) {
        System.out.println("RequestId: " + result.getRequestId());
        // Add your logic to process the speech recognition result here.
    }

    @Override
    public void onComplete() {
        System.out.println("Task complete");
    }

    @Override
    public void onError(Exception e) {
        System.out.println("Task failed: " + e.getMessage());
    }
};
Interface/MethodParameterReturn valueDescription
public void onEvent(RecognitionResult result)
result: Real-time recognition result (RecognitionResult)NoneCalled when the server sends a response.
public void onComplete()
NoneNoneCalled after the task completes.
public void onError(Exception e)
e: The exception information.NoneCalled when an exception occurs.

Response

Real-time recognition result (RecognitionResult)
RecognitionResult represents the result of a single real-time recognition.
Interface/MethodParameterReturn valueDescription
public String getRequestId()
NonerequestIdGets the requestId.
public boolean isSentenceEnd()
NoneWhether a complete sentence has been formed, that is, whether a sentence boundary was detected.Determines whether the given sentence has ended.
public Sentence getSentence()
NoneSentence information (Sentence)Gets the sentence information, including timestamps and text.
Sentence information (Sentence)
Interface/MethodParameterReturn valueDescription
public Long getBeginTime()
NoneThe sentence start time, in ms.Returns the sentence start time.
public Long getEndTime()
NoneThe sentence end time, in ms.Returns the sentence end time.
public String getText()
NoneThe recognized text.Returns the recognized text.
public List<Word> getWords()
NoneA List of Word-level timestamp information (Word) objects.Returns word-level timestamp information.
Word-level timestamp information (Word)
Interface/MethodParameterReturn valueDescription
public long getBeginTime()
NoneThe word start time, in ms.Returns the word start time.
public long getEndTime()
NoneThe word end time, in ms.Returns the word end time.
public String getText()
NoneThe word.Returns the recognized word.
public String getPunctuation()
NoneThe punctuation.Returns the punctuation.

Fun-ASR-Realtime

Key interfaces

Response

Error codes

If you encounter errors, see Error codes for troubleshooting. If the issue persists, join the developer community listed in the speech SDK sample repository to report your issue and provide the Request ID for further investigation.