Fun-ASR audio file recognition Android SDK

更新时间:
复制 MD 格式

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

  1. Get an API key: Get an API key and API host. For security, configure the API key as an environment variable.

  2. Download the SDK and run the sample code:

    • Download the latest SDK package.

    • Unzip the package. Obtain the SDK in AAR format from the app/libs directory and add it to your project dependencies.To use Android C++, use the android_libs and android_include directories 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

  1. Initialize the SDK.

  2. Configure parameters as needed.

  3. Call startFileTranscriber to start the recognition task (async_request set to false).

  4. In the onFileTransEventCallback interface, listen for the EVENT_FILE_TRANS_RESULT event to obtain the final recognition result.

  5. Call release to release SDK resources.

Asynchronous mode

  1. Initialize the SDK.

  2. Configure parameters as needed.

  3. Call startFileTranscriber to start the recognition job (async_request set to true).

  4. Call queryFileTranscriber to actively query the transcription progress or result.

  5. In the onFileTransEventCallback interface, listen for the EVENT_FILE_TRANS_QUERY_RESULT event to obtain the current query result.

  6. In the onFileTransEventCallback interface, listen for the EVENT_FILE_TRANS_RESULT event and retrieve the final recognition result.

  7. 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

    url

    String

    Yes

    Service endpoint. The value is fixed at wss://dashscope.aliyuncs.com/api-ws/v1/inference.

    apikey

    String

    Yes

    API key.

    service_mode

    String

    Yes

    Running mode. The value is fixed at "1" for audio file recognition.

    device_id

    String

    Yes

    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_path

    String

    No

    The storage path for log files.

    This parameter takes effect only when save_log is 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_size

    int

    No

    The maximum size of a log file in bytes.

    This parameter takes effect only when save_log is set to true in the initialize call.

    Default value: 104857600 (100 × 1024 × 1024 bytes, which is 100 MiB).

    log_track_level

    int

    No

    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_level and level (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 the log_track_level and level values. For example, if log_track_level is set to 2 (INFO) and level is 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_urls

    array[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, wmv

      Important

      Due 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_request

    boolean

    No

    Specifies whether the speech recognition request is asynchronous.

    Default value: false.

    Valid values:

    • true: asynchronous request

    • false: synchronous request

    apikey

    string

    No

    If the apikey in the Connection and control parameters uses a temporary API key, you can update it here to prevent it from expiring.

    nls_config

    object

    Yes

    The core configuration object for speech recognition. It includes key parameters such as model selection and recognition effect control.

    nls_config.model

    string

    Yes

    The speech recognition model.

    nls_config.special_word_filter

    object

    No

    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_id

    array[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.

    Important

    Each 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_enabled

    boolean

    No

    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_id field to distinguish speakers.

    Note

    If 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_count

    integer

    No

    A reference value for the number of speakers. To use this feature, set diarization_enabled to true.

    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_id

    string

    No

    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_hints

    array[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.

    Click to view the supported language codes

    • fun-asr, fun-asr-2025-11-07, fun-asr-mtl, fun-asr-mtl-2025-08-25:

      • 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-2025-08-25:

      • zh: Chinese

      • en: English

    nls_config.parameters

    object

    No

    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

    callback

    INativeFileTransCallback

    The implementation of the event and data callback interface.

    parameters

    String

    A JSON string that contains authentication, connection, and debugging parameters. For more information, see Connection and control parameters.

    level

    Constants.LogLevel

    Controls the logging level of the SDK itself.

    save_log

    boolean

    Specifies whether to save binary logs. If set to true, you must specify a path using debug_path in Connection and control parameters, and you can set the file size using max_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

    params

    String

    The nls_config parameter in Speech recognition effect parameters. Parameters other than nls_config cannot 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

    params

    String

    Speech 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_id

    byte[]

    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_id

    byte[]

    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_id

    byte[]

    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

    event

    NuiEvent

    The callback event.

    resultCode

    int

    Error code. This is valid when an EVENT_ASR_ERROR event occurs.

    asrResult

    AsrResult

    The speech recognition result.

    taskId

    String

    The task ID.

    arg2

    int

    Reserved 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.