Skip to main content

Qwen-ASR-Realtime Java SDK

This document describes how to use the DashScope Java SDK to call the Real-time Speech Recognition (Qwen-ASR-Realtime) model.

Model Studio has launched workspace-dedicated domains for the China (Beijing) and Singapore regions, which deliver superior performance and higher stability for inference requests. Migrating to the new domains 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
{WorkspaceId} must be replaced with your actual Workspace ID. The existing domains continue to work as before.

Prerequisites

  1. Install the SDK and make sure the DashScope SDK version is 2.22.5 or later.
  2. Obtain and configure an API Key.
  3. Learn about the WebSocket API.

QuickStart

The sample code reads your_audio_file.pcm (PCM16, 16 kHz, mono). If you only have formats such as MP3/WAV, you can convert them with ffmpeg:
ffmpeg -i your_audio.mp3 -ar 16000 -ac 1 -f s16le your_audio_file.pcm
Java
import com.alibaba.dashscope.audio.omni.*;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.google.gson.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sound.sampled.LineUnavailableException;
import java.io.File;
import java.io.FileInputStream;
import java.util.Base64;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;

public class Qwen3AsrRealtimeUsage {
    private static final Logger log = LoggerFactory.getLogger(Qwen3AsrRealtimeUsage.class);
    private static final int AUDIO_CHUNK_SIZE = 1024; // Audio chunk size in bytes
    private static final int SLEEP_INTERVAL_MS = 30;  // Sleep interval in milliseconds

    public static void main(String[] args) throws InterruptedException, LineUnavailableException {
        CountDownLatch finishLatch = new CountDownLatch(1);

        OmniRealtimeParam param = OmniRealtimeParam.builder()
                .model("qwen3-asr-flash-realtime")
                // The following is the configuration for the Singapore region. Replace "{WorkspaceId}" with your actual workspace ID. The configuration varies by region.
                .url("wss://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api-ws/v1/realtime")
                // The API Key differs between the Singapore and Beijing regions. Get your 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 Alibaba Cloud Model Studio API Key: .apikey("sk-xxx")
                .apikey(System.getenv("DASHSCOPE_API_KEY"))
                .build();

        OmniRealtimeConversation conversation = null;
        final AtomicReference<OmniRealtimeConversation> conversationRef = new AtomicReference<>(null);
        conversation = new OmniRealtimeConversation(param, new OmniRealtimeCallback() {
            @Override
            public void onOpen() {
                System.out.println("connection opened");
            }
            @Override
            public void onEvent(JsonObject message) {
                String type = message.get("type").getAsString();
                switch(type) {
                    case "session.created":
                        System.out.println("start session: " + message.get("session").getAsJsonObject().get("id").getAsString());
                        break;
                    case "conversation.item.input_audio_transcription.completed":
                        System.out.println("transcription: " + message.get("transcript").getAsString());
                        finishLatch.countDown();
                        break;
                    case "input_audio_buffer.speech_started":
                        System.out.println("======VAD Speech Start======");
                        break;
                    case "input_audio_buffer.speech_stopped":
                        System.out.println("======VAD Speech Stop======");
                        break;
                    case "conversation.item.input_audio_transcription.text":
                        System.out.println("transcription: " + message.get("text").getAsString() + message.get("stash").getAsString());
                        break;
                    default:
                        break;
                }
            }
            @Override
            public void onClose(int code, String reason) {
                System.out.println("connection closed code: " + code + ", reason: " + reason);
            }
        });
        conversationRef.set(conversation);
        try {
            conversation.connect();
        } catch (NoApiKeyException e) {
            throw new RuntimeException(e);
        }

        OmniRealtimeTranscriptionParam transcriptionParam = new OmniRealtimeTranscriptionParam();
        transcriptionParam.setLanguage("zh");
        transcriptionParam.setInputAudioFormat("pcm");
        transcriptionParam.setInputSampleRate(16000);

        OmniRealtimeConfig config = OmniRealtimeConfig.builder()
                .modalities(Collections.singletonList(OmniRealtimeModality.TEXT))
                .transcriptionConfig(transcriptionParam)
                .build();
        conversation.updateSession(config);

        String filePath = "your_audio_file.pcm";
        File audioFile = new File(filePath);
        if (!audioFile.exists()) {
            log.error("Audio file not found: {}", filePath);
            return;
        }

        try (FileInputStream audioInputStream = new FileInputStream(audioFile)) {
            byte[] audioBuffer = new byte[AUDIO_CHUNK_SIZE];
            int bytesRead;
            int totalBytesRead = 0;

            log.info("Starting to send audio data from: {}", filePath);

            // Read and send audio data in chunks
            while ((bytesRead = audioInputStream.read(audioBuffer)) != -1) {
                totalBytesRead += bytesRead;
                byte[] chunk = new byte[bytesRead];
                System.arraycopy(audioBuffer, 0, chunk, 0, bytesRead);
                String audioB64 = Base64.getEncoder().encodeToString(chunk);
                // Send audio chunk to conversation
                conversation.appendAudio(audioB64);

                // Add small delay to simulate real-time audio streaming
                Thread.sleep(SLEEP_INTERVAL_MS);
            }

            log.info("Finished sending audio data. Total bytes sent: {}", totalBytesRead);

        } catch (Exception e) {
            log.error("Error sending audio from file: {}", filePath, e);
        }

        //send session.finish and wait for finish and close
        conversation.endSession();
        log.info("task finished");

        System.exit(0);
    }
}
        Constants.baseHttpApiUrl = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1";

Request parameters

  • The following parameters are set through the chained methods of OmniRealtimeParam.
    OmniRealtimeParam param = OmniRealtimeParam.builder()
                    .model("qwen3-asr-flash-realtime")
                    // The following is the configuration for the Singapore region. Replace "{WorkspaceId}" with your actual workspace ID. The configuration varies by region.
                    .url("wss://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api-ws/v1/realtime")
                    // The API Key differs between the Singapore and Beijing regions. Get your API Key: https://www.alibabacloud.com/help/model-studio/get-api-key
                    // If the environment variable is unset, replace the next line with your Model Studio API key: .apikey("sk-xxx")
                    .apikey(System.getenv("DASHSCOPE_API_KEY"))
                    .build();
    

    Parameter

    Type

    Required

    Description

    model

    String

    Yes

    The name of the model to use.

    url

    String

    Yes

    Speech recognition service address:

    • China (Beijing) region: wss://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api-ws/v1/realtime. When making a call, replace {WorkspaceId} with your actual Workspace ID.

    • Singapore region: wss://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api-ws/v1/realtime. When making a call, replace {WorkspaceId} with your actual Workspace ID.

    apikey

    String

    No

    The API Key to configure.

  • The following parameters are set through the chained methods of OmniRealtimeConfig.
    OmniRealtimeTranscriptionParam transcriptionParam = new OmniRealtimeTranscriptionParam();
            transcriptionParam.setLanguage("zh");
            transcriptionParam.setInputSampleRate(16000);
            transcriptionParam.setInputAudioFormat("pcm");
    
    OmniRealtimeConfig config = OmniRealtimeConfig.builder()
                    .modalities(Collections.singletonList(OmniRealtimeModality.TEXT))
                    .enableTurnDetection( true)
                    .turnDetectionType("server_vad")
                    .turnDetectionThreshold(0.0f)
                    .turnDetectionSilenceDurationMs(400)
                    .transcriptionConfig(transcriptionParam)
                    .build();
    

    Parameter

    Type

    Required

    Description

    modalities

    List<OmniRealtimeModality>

    Yes

    The model output modality, fixed to [OmniRealtimeModality.TEXT].

    enableTurnDetection

    boolean

    No

    Whether to enable server-side voice activity detection (VAD). When it is disabled, you must manually call the commit() method to trigger recognition.

    Default value: true.

    Valid values:

    • true: Enable

    • false: Disable

    turnDetectionType

    String

    No

    The server-side VAD type, fixed as server_vad.

    turnDetectionThreshold

    float

    No

    The VAD detection threshold. We recommend setting this value to 0.0.

    Default value: 0.5.

    Value range: [-1, 1].

    A lower threshold increases the sensitivity of the VAD and may misclassify background noise as speech. A higher threshold reduces the sensitivity, which helps reduce false triggers in noisy environments.

    turnDetectionSilenceDurationMs

    int

    No

    The VAD sentence-break detection threshold (ms). Silence lasting longer than this threshold is considered the end of an utterance. We recommend setting this value to 400.

    Default value: 800.

    Value range: [200, 6000].

    A lower value (such as 300 ms) allows the model to respond faster, but may cause unreasonable sentence breaks at natural pauses. A higher value (such as 1200 ms) better handles pauses within long sentences, but increases the overall response latency.

    transcriptionConfig

    OmniRealtimeTranscriptionParam

    No

    Speech recognition related configurations.

  • The following parameters are set via the setter methods of OmniRealtimeTranscriptionParam.
    OmniRealtimeTranscriptionParam transcriptionParam = new OmniRealtimeTranscriptionParam();
            transcriptionParam.setLanguage("zh");
            transcriptionParam.setInputSampleRate(16000);
            transcriptionParam.setInputAudioFormat("pcm");
    

    Parameter

    Type

    Required

    Description

    language

    String

    No

    The source language of the audio.

    • zh: Chinese (Mandarin, Sichuan dialect, Minnan dialect, Wu dialect)

    • yue: Cantonese

    • en: English

    • ja: Japanese

    • de: German

    • ko: Korean

    • ru: Russian

    • fr: French

    • pt: Portuguese

    • ar: Arabic

    • it: Italian

    • es: Spanish

    • hi: Hindi

    • id: Indonesian

    • th: Thai

    • tr: Turkish

    • uk: Ukrainian

    • vi: Vietnamese

    • cs: Czech

    • da: Danish

    • fil: Filipino

    • fi: Finnish

    • is: Icelandic

    • ms: Malay

    • no: Norwegian

    • pl: Polish

    • sv: Swedish

    inputSampleRate

    int

    No

    Audio sampling rate (Hz). Supports 16000 and 8000.

    Default value: 16000.

    When set to 8000, the server first upsamples the audio to 16000 Hz before recognition, which may introduce slight latency. It is recommended only when the source audio is 8000 Hz (for example, telephone lines).

    inputAudioFormat

    String

    No

    Audio format. Supports pcm and opus.

    Default value: pcm.

API reference

OmniRealtimeConversation class

OmniRealtimeConversation is imported via import com.alibaba.dashscope.audio.omni.OmniRealtimeConversation;.
Method signatureServer response events (delivered via callbacks)Description
public OmniRealtimeConversation(OmniRealtimeParam param, OmniRealtimeCallback callback)
NoneConstructor.
public void connect() throws NoApiKeyException, InterruptedException
session.created
Session created
session.updated
Session configuration updated
Creates a connection with the server.
public void updateSession(OmniRealtimeConfig config)
session.updated
Session configuration updated
Used to update the session configuration. It is recommended to call this method first after the connection is established to apply settings. If this method is not called, the system uses the default configuration. Only the parameters involved in the request parameters need to be considered.
public void appendAudio(String audioBase64)
NoneAppends Base64-encoded audio data chunks to the cloud input audio buffer.
  • When enableTurnDetection in the request parameters is set to true, the audio buffer is used for speech detection, and the server decides when to commit.
  • When enableTurnDetection in the request parameters is set to false, the client can choose how much audio to place in each event, up to 15 MiB. For example, streaming smaller chunks from the client allows VAD to respond more quickly.
public void commit()
input_audio_buffer.committed
The server received the committed audio
Commit the audio and video previously added to the cloud-side buffer via append. If the input audio buffer is empty, an error will occur.Disabled scenarios: When enableTurnDetection in request parameters is set to true.
public void endSession() throws InterruptedException
session.finished
The server completes speech recognition and ends the session.
Notifies the server to end the session. After receiving the session-end notification, the server will complete the final speech recognition.When to call:endSessionAsync is the asynchronous version of endSession; the two are functionally identical.
public void close()
NoneTerminates the task and closes the connection.
public String getSessionId()
NoneGets the session_id of the current task.
public String getResponseId()
NoneGets the response_id of the most recent response.

Callback interface (OmniRealtimeCallback)

The server returns server response events and data to the client through callbacks. Extend this class and implement the corresponding methods to handle server events. Import it via import com.alibaba.dashscope.audio.omni.OmniRealtimeCallback;.
Method signatureParameterDescription
public void onOpen()
NoneTriggered when the WebSocket connection is successfully established.
public abstract void onEvent(JsonObject message)
message: Server eventsTriggered when a server event is received.
public abstract void onClose(int code, String reason)
code: status codereason: Log information generated when the WebSocket connection is closedTriggered when the WebSocket connection is closed.