This document provides a detailed guide on how to use the Fun-ASR audio file recognition Android software development kit (SDK) to convert speech to text.
User guide: Non-real-time speech recognition. For supported audio formats, file size limits, duration limits, and other input requirements, see Audio specifications.
Getting started
-
Get an API key: Get an API key and API host. For security, configure the API key as an environment variable.
-
Download the SDK and run the sample code:
-
Unzip the package. Obtain the SDK in AAR format from the
app/libsdirectory and add it to your project dependencies.To use Android C++, use theandroid_libsandandroid_includedirectories in the ZIP package to obtain the dynamic libraries and header files. -
Open the project in Android Studio. The sample code is in
DashFunAsrFileTranscriberActivity.java. Replace the API key to test the feature.
Call procedure
Synchronous mode
-
Initialize the SDK.
-
Configure parameters as needed.
-
Call startFileTranscriber to start the recognition task (
async_requestset tofalse). -
In the onFileTransEventCallback interface, listen for the
EVENT_FILE_TRANS_RESULTevent to obtain the final recognition result. -
Call release to release SDK resources.
Asynchronous mode
-
Initialize the SDK.
-
Configure parameters as needed.
-
Call startFileTranscriber to start the recognition job (
async_requestset totrue). -
Call queryFileTranscriber to actively query the transcription progress or result.
-
In the onFileTransEventCallback interface, listen for the
EVENT_FILE_TRANS_QUERY_RESULTevent to obtain the current query result. -
In the onFileTransEventCallback interface, listen for the
EVENT_FILE_TRANS_RESULTevent and retrieve the final recognition result. -
Call release to release SDK resources.
Request parameters
Connection and control parameters
Configure these parameters by passing a JSON string to the parameters parameter of the initialize interface.
-
Parameter example: The following is a sample JSON string. Not all parameters are listed. Add parameters as needed during encoding:
{ "url": "wss://dashscope.aliyuncs.com/api-ws/v1/inference", "apikey": "st-****", "device_id": "my_device_id", "service_mode": "1" } -
Parameter descriptions
Parameter
Type
Required
Description
urlStringYes
Service endpoint. The value is fixed at
wss://dashscope.aliyuncs.com/api-ws/v1/inference.apikeyStringYes
API key.
service_modeStringYes
Running mode. The value is fixed at
"1"for audio file recognition.device_idStringYes
A unique string that identifies the end user. You can set it to the user ID in your application or a unique device identifier generated by the client. This ID is mainly used for log tracking and troubleshooting.
debug_pathStringNo
The storage path for log files.
This parameter takes effect only when
save_logis set to true in the initialize call. If you enable logging, you must specify a log file path. Otherwise, an error occurs.A maximum of two log files are retained locally.
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 call.Default value: 104857600 (100 × 1024 × 1024 bytes, which is 100 MiB).
log_track_levelintNo
Controls the filter level for log content sent externally through the log callback (onFileTransLogTrackCallback).
Default value: 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: The
log_track_levelandlevel(set through the initialize interface) parameters together determine the final callback log. A log entry is sent to the callback only if its level value is greater than or equal to both thelog_track_levelandlevelvalues. For example, iflog_track_levelis set to 2 (INFO) andlevelis set to 3 (WARNING), only logs at the WARNING level or higher (value >= 3) are sent to the callback. -
Speech recognition effect parameters
Configure the nls_config parameter using the setParams interface, or configure all speech recognition effect parameters using the startFileTranscriber interface.
-
Parameter example: The following is a sample JSON string. Not all parameters are listed. Add parameters as needed during encoding:
{ "file_urls": [ "https://dashscope.oss-cn-beijing.aliyuncs.com/samples/audio/paraformer/hello_world_female2.wav" ], "async_request": false, "nls_config": { "model":"fun-asr", "diarization_enabled": false, "parameters": { "speech_noise_threshold": 0.0 } } } -
Parameter descriptions
Parameter
Type
Required
Description
file_urlsarray[string]Yes
A list of URLs for the audio or video files to be transcribed. The HTTP and HTTPS protocols are supported. A single request supports only 1 URL.
If your audio files are stored in OSS, the SDK does not support temporary URLs that start with the oss:// prefix.
-
Audio formats:
aac,amr,avi,flac,flv,m4a,mkv,mov,mp3,mp4,mpeg,ogg,opus,wav,webm,wma,wmvImportantDue to the large number and variety of audio and video formats, it is technically impossible to test all of them. The API cannot guarantee that all formats can be correctly recognized. Test your files to verify that you can obtain normal speech recognition results.
-
Audio sampling rate: Any
-
Audio file size and duration: The audio file cannot exceed 2 GB. The duration must be within 12 hours.
If the file you want to process exceeds these limits, you can try to pre-process the file to reduce its size. For more information about best practices for file pre-processing, see Pre-process video files to improve transcription efficiency (for audio file recognition scenarios).
async_requestbooleanNo
Specifies whether the speech recognition request is asynchronous.
Default value:
false.Valid values:
-
true: asynchronous request
-
false: synchronous request
apikeystringNo
If the
apikeyin the Connection and control parameters uses a temporary API key, you can update it here to prevent it from expiring.nls_configobjectYes
The core configuration object for speech recognition. It includes key parameters such as model selection and recognition effect control.
nls_config.modelstringYes
The speech recognition model.
nls_config.special_word_filterobjectNo
Specifies the sensitive words to be processed during speech recognition and supports different processing methods for different sensitive words.
If this parameter is not passed, the system's built-in sensitive word filtering logic is enabled. Words in the recognition result that match the Alibaba Cloud Model Studio sensitive word list are replaced with asterisks (
*) of the same length.If this parameter is passed, the following sensitive word processing policies can be implemented:
-
Replace with
*: Replaces matched sensitive words with asterisks (*) of the same length. -
Filter directly: Completely removes matched sensitive words from the recognition result.
The value of this parameter should be a JSON object with the following structure:
{ "filter_with_signed": { "word_list": ["test"] }, "filter_with_empty": { "word_list": ["start", "happen"] }, "system_reserved_filter": true }JSON field descriptions:
-
filter_with_signed-
Type: object.
-
Required: No.
-
Description: Configures the list of sensitive words to be replaced with
*. Matched words in the recognition result are replaced with asterisks (*) of the same length. -
Example: Based on the JSON above, the speech recognition result for "Help me test this piece of code" will be "Help me ** this piece of code".
-
Internal fields:
-
word_list: A string array that lists the sensitive words to be replaced.
-
-
-
filter_with_empty-
Type: object.
-
Required: No.
-
Description: Configures the list of sensitive words to be removed (filtered) from the recognition result. Matched words in the recognition result are completely deleted.
-
Example: Based on the JSON above, the speech recognition result for "Is the game about to start?" will be "Is the game about to?".
-
Internal fields:
-
word_list: A string array that lists the sensitive words to be completely removed (filtered).
-
-
-
system_reserved_filter-
Type: Boolean value.
-
Required: No.
-
Default value: true.
-
Description: Specifies whether to enable the system's preset sensitive word rules. If set to
true, the system's built-in sensitive word filtering logic is also enabled. Words in the recognition result that match the Alibaba Cloud Model Studio sensitive word list are replaced with asterisks (*) of the same length.
-
nls_config.channel_idarray[integer]No
Indexes of sound channels to recognize in a multi-channel audio file. The index starts from 0. For example, [0] recognizes the first channel, and [0, 1] recognizes the first and second channels. If omitted, the first channel is processed by default.
ImportantEach specified sound channel is billed separately. For example, a request for [0, 1] for a single file incurs two separate charges.
Default value:
[0].nls_config.diarization_enabledbooleanNo
Automatic speaker diarization is disabled by default. This feature applies to single-channel audio only (not supported for multi-channel audio).
When enabled, recognition results include the
speaker_idfield to distinguish speakers.NoteIf speaker diarization is enabled, keep the audio duration under 2 hours. Audio exceeding 2 hours may cause recognition failures or timeouts.
For an example of
speaker_id, see Recognition result description.nls_config.speaker_countintegerNo
A reference value for the number of speakers. To use this feature, set
diarization_enabledtotrue.By default, the number of speakers is automatically determined. If you configure this parameter, it can only assist the algorithm in trying to output the specified number of speakers, but it cannot guarantee that this number will be output.
Valid values:
[2, 100]. This feature is used to distinguish multiple speakers, so a minimum of 2 must be set.nls_config.vocabulary_idstringNo
The ID of a hotword vocabulary, used to improve the recognition accuracy of specific words. This parameter is applicable to v2 and later model versions. For more information about how to use hotwords, see Customize hotwords.
nls_config.language_hintsarray[string]No
The language code for recognition. If the source language is unknown, leave it unset and the model detects the language automatically.
The system reads only the first value in the array. Any extra values are ignored.
nls_config.parametersobjectNo
Configures other parameters. The content is in JSON object format.
-
Key interfaces
NativeNui
initialize
Initializes the speech recognition SDK instance. The SDK uses a singleton pattern. Do not initialize it again before calling release.
This interface is blocking and should be called in a non-UI thread.
-
Method signature
public synchronized int initialize(final INativeFileTransCallback callback, String parameters, final Constants.LogLevel level, final boolean save_log) -
Parameter descriptions
Parameter
Type
Description
callbackINativeFileTransCallbackThe implementation of the event and data callback interface.
parametersStringA JSON string that contains authentication, connection, and debugging parameters. For more information, see Connection and control parameters.
levelConstants.LogLevelControls the logging level of the SDK itself.
save_logbooleanSpecifies whether to save binary logs. If set to
true, you must specify a path usingdebug_pathin Connection and control parameters, and you can set the file size usingmax_log_file_size. -
Return value description
Returns an error code. For more information, see Error codes.
setParams
This interface is used to independently set or update the nls_config parameter. If all parameters are provided at once in startFileTranscriber, you do not need to call this method.
-
Method signature
public synchronized int setParams(String params); -
Parameter descriptions
Parameter
Type
Description
paramsStringThe
nls_configparameter in Speech recognition effect parameters. Parameters other thannls_configcannot be set using this method.Example:
{ "nls_config": { "model":"fun-asr", "diarization_enabled": false } } -
Return value description
Returns an error code. For more information, see Error codes.
startFileTranscriber
Start the detection.
-
Method signature
public synchronized int startFileTranscriber(String params, byte[] task_id) -
Parameter descriptions
Parameter
Type
Description
paramsStringSpeech recognition effect parameters.
Example:
{ "file_urls": [ "https://dashscope.oss-cn-beijing.aliyuncs.com/samples/audio/paraformer/hello_world_female2.wav" ], "async_request": false, "nls_config": { "model":"fun-asr", "diarization_enabled": false } }task_idbyte[]The task ID. The SDK internally generates a random string. You can obtain the task_id after this interface is called successfully.
-
Return value description
Returns an error code. For more information, see Error codes.
queryFileTranscriber
This interface is used to actively query the current status and result of an asynchronous task. After a successful call, the result is returned through the onFileTransEventCallback callback in the EVENT_FILE_TRANS_QUERY_RESULT event.
-
Method signature
public synchronized int queryFileTranscriber(String task_id) -
Parameter descriptions
Parameter
Type
Description
task_idbyte[]The ID of the task to query.
-
Return value description
Returns an error code. For more information, see Error codes.
cancelFileTranscriber
Immediately cancels the current task.
-
Method signature
public synchronized int cancelFileTranscriber(String task_id) -
Parameter descriptions
Parameter
Type
Description
task_idbyte[]The ID of the task to cancel.
-
Return value description
Returns an error code. For more information, see Error codes.
release
Releases all internal resources of the SDK. After this method is called, the SDK instance becomes unavailable. To use it again, you must call initialize to re-initialize it.
-
Method signature
public synchronized int release(); -
Return value description
Returns an error code. For more information, see Error codes.
GetVersion
Gets the current SDK version information.
-
Method signature
public synchronized String GetVersion(); -
Return value description
The current SDK version information.
INativeFileTransCallback: Listener callback
onFileTransEventCallback: Listen for events and speech recognition results
-
Method signature
void onFileTransEventCallback(NuiEvent event, final int resultCode, final int arg2, AsrResult asrResult, String taskId); -
Parameter descriptions
Parameter
Type
Description
eventThe callback event.
resultCodeintError code. This is valid when an EVENT_ASR_ERROR event occurs.
asrResultAsrResultThe speech recognition result.
taskIdStringThe task ID.
arg2intReserved parameter.
onFileTransLogTrackCallback: Listen for tracking logs
This callback is used to receive detailed internal logs from the SDK for troubleshooting and debugging.
default void onFileTransLogTrackCallback(Constants.LogLevel level, String log)
NuiEvent: Event types
|
Event |
Description |
|
EVENT_FILE_TRANS_CONNECTED |
Successfully connected to the service. |
|
EVENT_FILE_TRANS_UPLOADED |
Successfully uploaded the audio file to be recognized. |
|
EVENT_FILE_TRANS_QUERY_RESULT |
Query task result. |
|
EVENT_FILE_TRANS_RESULT |
Final recognition result. |
|
EVENT_ASR_ERROR |
An error occurred during speech recognition. |