Qwen-Audio-Realtime Android SDK
Use Qwen-Audio-Realtime to conduct real-time voice conversations with Android SDK, enabling real-time audio input as well as voice or text output.
For connection and authentication, see WebSocket access. For event fields and JSON examples, see Client events and Server events.
User guide: For model introductions and selection advice, see Real-time voice conversation.
Quick start
- Obtain and configure an API key.
-
Download the SDK and run the sample code:
- Download the latest SDK package.
- Extract the ZIP package. Obtain the AAR SDK from
app/libsand add it to your project dependencies. For Android C++ integration, useandroid_libsandandroid_includein the ZIP package to obtain the dynamic libraries and header files. - Open the project in Android Studio. The sample code is in
DashQwenAudioChatActivity.java. Replace the API key to try the feature.
Call procedure
- Initialize the SDK.
- Set parameters based on your business needs: use the
parametersargument of the initialize method to set the connection and control parameters, and use the setParams method to set the voice conversation parameters. - Call startDialog to start the conversation.
- In the onNuiAudioStateChanged callback, start the recording device based on the audio state.
- Continuously provide recording data in the onNuiNeedAudioData callback, or continuously push recording data through updateAudio.
- Continuously receive the audio data returned by the model in the onNuiAssistEventCallback callback.
- Listen for events and obtain event information in the onNuiEventCallback callback.
- Call stopDialog to stop the conversation, and confirm that the conversation has ended by listening for the EVENT_TRANSCRIBER_COMPLETE event.
- When the conversation feature is no longer used, call release to release the SDK resources.
Request parameters
Connection and control parameters
Pass a JSON string in the parameters argument of initialize.
Example: The following JSON string does not list every parameter. Add parameters as needed for your use case.
The example uses qwen-audio-3.1-realtime-plus. To use another model, update both model in the URL and nls_config.model in Voice conversation parameters.
{
"url": "wss://dashscope.aliyuncs.com/api-ws/v1/realtime?model=qwen-audio-3.1-realtime-plus",
"apikey": "st-****",
"device_id": "my_device_id",
"service_mode": "1"
}
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
|
| Yes | Service endpoint:
{WorkspaceId} with your actual Workspace ID. |
|
| Yes | API key. |
|
| Yes | Runtime mode. Set this parameter to |
|
| Yes | A unique string that identifies the end user. You can use an in-app user ID or a client-generated device identifier. This ID is mainly used for log tracing and troubleshooting. |
audio_update_manually |
| No | Whether to enable the active audio data push mode. Default: "false". |
|
| No | When audio_update_manually is set to "true" and an on-device audio capability (such as AEC or VAD) is enabled, you must set workspace, which is the path where on-device resource files are stored. |
|
| No | The storage path of log files. This parameter takes effect only when |
|
| No | Whether to save audio files for debugging. The audio files are saved under
save_log is set to true when calling the initialize method. In this case, debug_path must also be set. |
|
| No | The maximum size of log files in bytes. |
|
| No | The filter level that controls the log content sent through the log callback (
log_track_level and level (set through the initialize method) jointly determine the logs that are finally returned. A log is returned only when its level value is greater than or equal to both log_track_level and level. For example, if log_track_level is set to 2 (INFO) and level is set to 3 (WARNING), only logs at the WARNING level and above (values >= 3) are returned. |
aec_params |
| No | Advanced configuration object for the on-device AEC capability. This configuration object is enabled only when audio_update_manually is set to "true". |
aec_params.enable_aec |
| No | Whether to enable the on-device AEC echo cancellation capability. |
aec_params.save_audio |
| No | Whether to enable the audio storage feature of the on-device AEC echo cancellation module. When |
aec_params.enable_aec_data_callback |
| No | Whether to send the AEC-processed data to the user. Default: false. After it is enabled, the data is received in EVENT_AEC_DATA of onNuiAssistEventCallback. |
vad_params |
| No | Advanced configuration object for the on-device VAD capability. |
vad_params.enable_vad |
| No | Whether to enable the on-device VAD human voice detection capability. |
vad_params.save_audio |
| No | Whether to enable the audio storage feature of the on-device VAD human voice detection module. |
Voice conversation parameters
Pass a JSON string in the params argument of setParams.
Example: The following JSON string does not list every parameter. Add parameters as needed for your use case.
{
"service_type": 4,
"nls_config": {
"model": "qwen-audio-3.1-realtime-plus",
"sr_format": "pcm"
}
}
Parameters
| Top-level parameter | Type | Required | Description |
|---|---|---|---|
|
| Yes | Voice service type. Set this parameter to |
|
| Yes | Core voice conversation configuration, including model selection and conversation behavior. |
|
| Yes | Model name. Supports the |
|
| Yes | Input audio format. Currently only |
|
| No | A string in array format. The output modality settings of the model. Valid values:
|
nls_config.voice |
| No | TTS voice name. The default is
|
nls_config.enable_speech_emotion |
| No | Whether to enable enhanced emotional expression. When enabled, the response voice has more noticeable emotional variation. Default: |
nls_config.instructions |
| No | System instructions that define the model's role, response style, and behavioral preferences for the entire session. |
nls_config.max_history_turns |
| No | Maximum number of historical question-answer turns allowed in a request. Valid values: 1 to 50. Default: |
nls_config.tools |
| No | A string in array format. A list of Function Calling tool definitions. After this parameter is configured, the model can decide whether to call a tool based on the user input. |
nls_config.turn_detection |
| No | The turn detection fields described below apply to A string containing a JSON object for turn detection. If omitted, the session uses push-to-talk mode, in which audio is committed and inference is triggered manually. If set, duplex conversation mode is enabled. |
nls_config.turn_detection.type |
| No | VAD type. Valid values:
|
nls_config.turn_detection.threshold | float | No | VAD sensitivity. Applies to |
nls_config.turn_detection.silence_duration_ms |
| No | The minimum silence duration after speech ends, in milliseconds, effective only in server_vad mode (no effect in smart_turn and smart_turn_v2 modes). The model response is triggered when the silence reaches this duration. The lower the value, the faster the response, but brief pauses may also be falsely triggered. |
nls_config.turn_detection.voiceprint_audio_urls |
| No | A string in array format. Effective in smart_turn and smart_turn_v2 modes. A list of publicly accessible URLs of prerecorded audio for the target user, used for speaker enhancement. The model uses these audio files to lock onto the target speaker in duplex conversations, ignoring other speakers' voices and background noise. Up to 5 URLs are supported. Audio format requirements: 16kHz PCM or WAV. |
Key APIs
NativeNui
initialize
Initializes a voice conversation SDK instance. The SDK is a singleton. Do not initialize it more than once before you call release.
This method blocks, so call it on a non-UI thread.
Method signaturepublic synchronized int initialize(final INativeNuiCallback callback,
String parameters,
final Constants.LogLevel level,
final boolean save_log)
Parameters
| Parameter | Type | Description |
|---|---|---|
|
| The implementation of the event and data callback interface. |
|
| A JSON string that contains the authentication, connection, and debug parameters. See Connection and control parameters. |
|
| Controls the print level of the SDK's own logs. |
|
| Whether to save local logs. If set to |
setParams
Sets the Voice conversation parameters in JSON format. Call this method before startDialog.
Method signaturepublic synchronized int setParams(String params)
Parameters
| Parameter | Type | Description |
|---|---|---|
|
|
startDialog
Starts the conversation.
Method signaturepublic synchronized int startDialog(VadMode vad_mode, String dialog_params)
Parameters
| Parameter | Type | Description |
|---|---|---|
|
| VAD mode. Fixed to |
|
| If the |
stopDialog
Ends the conversation. After you call this method, the server returns the final conversation result and ends the task.
Method signaturepublic synchronized int stopDialog();
cancelDialog
Ends the conversation immediately. After you call this method, the task ends at once without waiting for the server to return the final conversation result.
Method signaturepublic synchronized int cancelDialog();
updateAction
Sends a conversation action command during an interaction to update runtime behavior such as the conversation context.
Method signaturepublic synchronized int updateAction(String params);
Parameters
| Parameter | Type | Description |
|---|---|---|
|
| A JSON string used to update runtime behavior such as the conversation context. |
|
| Fixed to "action". |
|
| The specific runtime command. Currently supports
|
|
| Updates the function call request when |
|
| Event type. This parameter must be set when
|
|
| Required when |
|
| Can be set when |
context.item parameters:
| Parameter | Type | Description |
|---|---|---|
id |
| Optional. The unique identifier of the conversation item. If not passed, the server generates it automatically. If the specified ID already exists in the conversation, an error is returned. |
type |
| Required. The conversation item type. Valid values:
|
role |
| Required for the |
content | array | Required for the
output_text: assistant text output. The required field is text. |
call_id |
| (Required for the |
name |
| (Required for the |
arguments |
| (Required for the |
output |
| (Required for the |
context.response parameters:
| Parameter | Type | Description |
|---|---|---|
modalities |
| A string in array format. The output modality settings of the model. Valid values:
|
voice | string | Overrides the TTS voice for this round. |
Example:
{
"type": "action",
"command": "function_call",
"context": {
"item": {
"call_id": "call_xxxx",
"output": "{\"city\":\"杭州\",\"condition\":\"晴\",\"temperature\":18}",
"type": "function_call_output"
},
"type": "conversation.item.create"
}
}
{
"type": "action",
"command": "function_call",
"context": {
"response": {
"modalities": [
"text",
"audio"
]
},
"type": "response.create"
}
}
updateAudio
When audio_update_manually is set to "true", recording data is no longer filled in through onNuiNeedAudioData. Instead, this method is used to actively push the data.
Method signaturepublic synchronized int updateAudio(byte[ ] data, int len,
boolean first_pack);
Parameters
| Parameter | Type | Description |
|---|---|---|
|
| The audio data to push. |
|
| The number of bytes of the pushed audio data. |
|
| Please ignore this parameter. You do not need to pay attention to it. |
updateRefAudio
When audio_update_manually is set to "true" and the on-device AEC echo cancellation capability is enabled, you need to use this method to push the audio data played by the player as the reference signal.
Method signaturepublic synchronized int updateRefAudio(byte[ ] data, int len,
boolean first_pack);
Parameters
| Parameter | Type | Description |
|---|---|---|
|
| The audio data to push. |
|
| The number of bytes of the pushed audio data. |
|
| Please ignore this parameter. You do not need to pay attention to it. |
release
Releases all internal resources of the SDK. After you call this method, the SDK instance becomes unavailable. To use it again, you must reinitialize it by calling initialize.
Method signaturepublic synchronized int release();
GetVersion
Gets the current SDK version information.
Method signaturepublic synchronized String GetVersion();
Return value
The current SDK version information.
INativeNuiCallback: listener callbacks
onNuiEventCallback: listen for event information
Method signaturevoid onNuiEventCallback(NuiEvent event, final int resultCode, final int arg2, KwsResult kwsResult, AsrResult asrResult);
Parameters
| Parameter | Type | Description |
|---|---|---|
|
| The callback event. |
|
| Valid only when the |
|
| A reserved parameter. |
|
| The speech recognition result. |
|
| The voice wake-up feature. You do not need to pay attention to this parameter. |
onNuiAudioStateChanged: listen for the audio state
The SDK uses this callback to notify you when to start or stop recording.
Method signaturevoid onNuiAudioStateChanged(AudioState state);
AudioState states
| State | Description |
|---|---|
| The interaction has started. You can open the recording device and start recording. |
| The interaction has stopped. You can stop recording. |
| The SDK instance has been released. You can completely close the recording device. |
onNuiNeedAudioData: supply audio data
After the conversation starts, this callback is triggered continuously, and you need to provide the audio data to be processed in it. You do not need to pay attention to this callback when audio_update_manually is set to "true".
Method signatureint onNuiNeedAudioData(byte[ ] buffer, int len);
Parameters
| Parameter | Type | Description |
|---|---|---|
|
| The audio data to fill. |
|
| The number of bytes of the filled audio data. |
The number of bytes actually filled.
onNuiAssistEventCallback: receive auxiliary events and data
This callback receives auxiliary events and related data from the SDK.
Method signaturevoid onNuiAssistEventCallback_(int event, byte[ ] info, int info_len,
byte[ ] data);
Parameters
| Parameter | Type | Description |
|---|---|---|
|
| A |
|
| You do not need to pay attention to this parameter. |
|
| You do not need to pay attention to this parameter. |
|
| Auxiliary data, such as the TTS audio returned by the AI. |
onNuiLogTrackCallback: listen for trace logs
This callback receives detailed internal logs from the SDK to help with troubleshooting and debugging.
default void onNuiLogTrackCallback(Constants.LogLevel level, String log)
NuiEvent: event types
| Event | Description |
|---|---|
EVENT_TRANSCRIBER_STARTED | The task started successfully. |
EVENT_VAD_START | Triggered immediately after the task starts. This does not mean that the start of speech has been detected. |
EVENT_VAD_END | The end of speech was detected. |
EVENT_ASR_PARTIAL_RESULT | An intermediate speech recognition result. |
EVENT_ASR_ERROR | An error occurred during the voice conversation. |
EVENT_MIC_ERROR | Triggered when no audio data is received for two consecutive seconds. |
EVENT_SENTENCE_END | The end of a sentence was detected and a complete recognition result was returned. |
EVENT_TRANSCRIBER_COMPLETE | The voice conversation ended. |
EVENT_AUDIO_TRANSCRIPTION | An incremental text subtitle event in audio mode. Subtitle segments are returned in streaming mode. |
EVENT_AUDIO_TRANSCRIPTION_COMPLETED | A subtitle output completion event in audio mode. |
EVENT_OTHER_RESULT | Other uncategorized event information, such as function_call results. |
EVENT_ASR_TTS_START | The AI starts returning TTS data. |
EVENT_ASR_TTS_DATA | The TTS data returned by the AI. |
EVENT_ASR_TTS_COMPLETE | The AI has finished returning TTS data. |
EVENT_AEC_DATA | The audio data after AEC echo cancellation. |