This guide shows you how to use the Android SDK for Qwen-Audio-3.0-ASR-Flash-Streaming and Fun-ASR-Realtime real-time speech recognition to convert speech to text.
User guide: For model introductions and selection advice, see Speech-to-text.
Quick start
Download the SDK and run the sample code:
Extract the ZIP package. The AAR-format SDK is in the
app/libsdirectory. Add it to your project dependencies. For Android C++ integration, useandroid_libsandandroid_includefrom the ZIP package to get the dynamic libraries and header files.Open the project in Android Studio. The sample code is in
DashFunAsrSpeechTranscriberActivity.java. Replace the API key to try out the feature.
Call procedure
Initialize the SDK.
Set the parameters for your use case. Use the
parametersargument of the initialize method to set the Connection and control parameters, and use the setParams method to set the Speech recognition parameters.Call startDialog to start recognition.
In the onNuiAudioStateChanged callback, start the recording device based on the audio state.
In the onNuiNeedAudioData callback, continuously supply recording data.
In the onNuiEventCallback callback, listen for events and get the speech recognition results.
Call stopDialog to stop recognition, and listen for the EVENT_TRANSCRIBER_COMPLETE event to confirm that recognition has ended.
When you no longer need recognition, call release to release the SDK resources.
Request parameters
Connection and control parameters
To configure these parameters, pass a JSON string in the parameters argument of the initialize method.
Example: The following is a JSON string example. Not all parameters are listed. Add others as needed when you write your code:
{ "url": "wss://dashscope.aliyuncs.com/api-ws/v1/inference", "apikey": "st-****", "device_id": "my_device_id", "service_mode": "1" }Parameters
Parameter
Type
Required
Description
urlStringYes
The service address. Fixed to
wss://dashscope.aliyuncs.com/api-ws/v1/inference.apikeyStringYes
The API key.
service_modeStringYes
The run mode. Fixed to
"1"for real-time speech recognition.device_idStringYes
A unique string that identifies the end user. You can set it to an in-app user ID or a client-generated unique device identifier. This ID is mainly used for log tracing and troubleshooting.
debug_pathStringNo
The storage path for log files.
This parameter takes effect only when
save_logis set to true in the initialize method. In this case, you must set the log file path, or an error occurs.A maximum of two log files are kept locally.
save_wavStringNo
Whether to save the debug audio file. The audio file is saved under
debug_path.Default: "false".
Valid values:
"true": save the file.
"false": do not save the file.
This parameter takes effect only when
save_logis set to true in the initialize method. In addition,debug_pathmust also be set.max_log_file_sizeintNo
The maximum size of a log file, in bytes.
This parameter takes effect only when
save_logis set to true in the initialize method.Default: 104857600 (100 * 1024 * 1024 bytes, that is, 100 MiB).
log_track_levelintNo
The filter level for the log content sent through the log callback (
onNuiLogTrackCallback).Default: 2.
Valid values:
0: LOG_LEVEL_VERBOSE
1: LOG_LEVEL_DEBUG
2: LOG_LEVEL_INFO
3: LOG_LEVEL_WARNING
4: LOG_LEVEL_ERROR
5: LOG_LEVEL_NONE (disables this feature)
Note:
log_track_levelandlevel(set through the initialize method) together determine which logs are ultimately sent to the callback. A log is sent to the callback only when its level value is greater than or equal to bothlog_track_levelandlevel. For example, iflog_track_levelis set to 2 (INFO) andlevelis set to 3 (WARNING), then only logs of WARNING level or higher (value >= 3) are sent to the callback.
Speech recognition parameters
To configure these parameters, pass a JSON string in the params argument of the setParams method.
Example: The following is a JSON string example. Not all parameters are listed. Add others as needed when you write your code:
{ "service_type": 4, "nls_config": { "model": "qwen-audio-3.0-asr-flash-streaming", "sr_format": "pcm", "sample_rate": "16000", "parameters": { "speech_noise_threshold": 0.0 } } }Parameters
Top-level parameter
Type
Required
Description
service_typeintYes
The speech service type. Fixed to
4for real-time speech recognition.nls_configobjectYes
The core configuration object for speech recognition. It contains key parameters such as model selection and recognition-quality controls.
nls_config.modelstringYes
The model name. The Qwen-Audio-3.0-ASR-Flash-Streaming and Fun-ASR-Realtime model series are supported. For details, see Supported models and regions.
nls_config.sr_formatstringYes
The audio format.
Valid values:
-
pcm -
wav -
mp3 -
opus -
speex -
aac -
amr
Importantopus/speex: Must use Ogg encapsulation.
wav: Must use PCM encoding.
amr: Only the AMR-NB type is supported.
nls_config.sample_rateintYes
The sample rate, in Hz.
Valid values: 8 kHz models support only 8000 Hz; other models support any sample rate.
nls_config.semantic_punctuation_enabledbooleanNo
Whether to enable semantic segmentation.
Default value: false.
-
true: Enables semantic segmentation and disables VAD segmentation.
-
false (default): Enables VAD segmentation and disables semantic segmentation.
Semantic segmentation is more accurate and is better suited to meeting transcription scenarios. VAD (Voice Activity Detection) segmentation has lower latency and is better suited to interactive scenarios.
nls_config.max_sentence_silenceintNo
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. When
semantic_punctuation_enabledis set to true, this parameter is not used as the criterion for returningsentence_end, but setting it too low may affect recognition performance.Default value: 1300.
Valid values: [200, 6000].
nls_config.multi_threshold_mode_enabledbooleanNo
ImportantTakes effect only when
semantic_punctuation_enabledis false.Whether to enable multi-threshold mode. When enabled, this prevents VAD segments from becoming too long.
Default value: false.
nls_config.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): The connection is disconnected due to timeout after 60 seconds, even if silent audio is sent continuously.
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.
nls_config.vocabulary_idstringNo
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.
nls_config.parameters.vocabularyobjectNo
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 configured together with precompiled hot words, only the instant hot words take effect. For usage details, see Instant hotwords.
ImportantOnly
qwen-audio-3.0-asr-flash-streamingsupports instant hot words.nls_config.language_hintsarray[string]No
The language of the audio to recognize. There is no default value; if not set, the model detects the language automatically.
For the Qwen-Audio-3.0-ASR-Flash-Streaming model series, you can set up to 4 values; if you set more than 4, only the first 4 take effect. For the Fun-ASR-Realtime model series, you can set only 1 value; if you set multiple values, only the first one takes effect.
nls_config.parametersobjectNo
Additional parameters, in JSON object format.
nls_config.parameters.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).
nls_config.parameters.special_word_filterobjectNo
Specifies the sensitive words to process during speech recognition, and supports setting different processing methods for different sensitive words. For details, see Sensitive word filtering.
-
Key interfaces
NativeNui
initialize
Initializes a speech recognition 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 signature
public synchronized int initialize(final INativeNuiCallback callback, String parameters, final Constants.LogLevel level, final boolean save_log)Parameters
Parameter
Type
Description
callbackThe implementation of the event and data callback interface.
parametersStringA JSON string that contains the authentication, connection, and debug parameters. See Connection and control parameters.
levelConstants.LogLevelControls the print level of the SDK's own logs.
save_logbooleanWhether to save local logs. If set to
true, specify the path throughdebug_pathin the Connection and control parameters, and optionally set the file size throughmax_log_file_size.Return value
An error code. See Error code reference.
setParams
Sets the Speech recognition parameters in JSON format. Call this method before startDialog.
Method signature
public synchronized int setParams(String params)Parameters
Parameter
Type
Description
paramsStringReturn value
An error code. See Error code reference.
startDialog
Starts recognition.
Method signature
public synchronized int startDialog(VadMode vad_mode, String dialog_params)Parameters
Parameter
Type
Description
vad_modeVadModeThe VAD mode. Fixed to
VadMode.TYPE_P2T.dialog_paramsStringIf the
apikeyparameter in Connection and control parameters uses a temporary API key, you can update it here when it expires.The content is in JSON format:
{ "apikey": "st-****" }Return value
An error code. See Error code reference.
stopDialog
Ends recognition. After you call this method, the server returns the final recognition result and ends the task.
Method signature
public synchronized int stopDialog();Return value
An error code. See Error code reference.
cancelDialog
Ends recognition immediately. After you call this method, the task ends at once without waiting for the server to return the final recognition result.
Method signature
public synchronized int cancelDialog();Return value
An error code. See Error code reference.
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 signature
public synchronized int release();Return value
An error code. See Error code reference.
GetVersion
Gets the current SDK version information.
Method signature
public synchronized String GetVersion();Return value
The current SDK version information.
INativeNuiCallback: listener callbacks
onNuiEventCallback: listen for events and speech recognition results
Method signature
void onNuiEventCallback(NuiEvent event, final int resultCode, final int arg2, KwsResult kwsResult, AsrResult asrResult);Parameters
Parameter
Type
Description
eventThe callback event.
resultCodeintThe error code. Valid when the EVENT_ASR_ERROR event occurs.
asrResultAsrResultThe speech recognition result.
kwsResultKwsResultThe voice wake-up feature. You do not need to use this parameter.
arg2intA reserved parameter.
onNuiAudioStateChanged: listen for the audio state
The SDK uses this callback to notify you when to start or stop recording.
Method signature
void onNuiAudioStateChanged(AudioState state);AudioState states
State
Description
STATE_OPENThe interaction has started. You can open the recording device and start recording.
STATE_PAUSEThe interaction has stopped. You can stop recording.
STATE_CLOSEThe SDK instance has been released. You can fully close the recording device.
onNuiNeedAudioData: supply the audio data to recognize
After recognition starts, this callback is triggered continuously. Supply the audio data to recognize in this callback.
Method signature
int onNuiNeedAudioData(byte[] buffer, int len);Parameters
Parameter
Type
Description
bufferbyte[]The audio data to fill.
lenintThe number of bytes of audio data to fill.
Return value
The number of bytes actually filled.
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 right after the task starts. This does not mean that the start of speech is detected. |
EVENT_VAD_END | The end of speech is detected. |
EVENT_ASR_PARTIAL_RESULT | An intermediate speech recognition result. |
EVENT_ASR_ERROR | An error occurred during speech recognition. |
EVENT_MIC_ERROR | Triggered when no audio data is received for 2 consecutive seconds. |
EVENT_SENTENCE_END | The end of a sentence is detected. A complete recognition result for the sentence is returned. |
EVENT_TRANSCRIBER_COMPLETE | Speech recognition has ended. |