This section describes the authentication, media configuration, connection establishment, and task-event handling processes for accessing Fun-ASR-Realtime via AOQ.
When accessing Fun-ASR-Realtime via AOQ, the client uploads audio via the Audio track and sends task events while receiving recognition results via the Data track.
Set the environment variable
If using Token Plan, the
Use
Wait for
Serialize the following JSON into UTF-8, write it into
Read
Read the recognition text from
After recording ends, stop capturing new audio and wait for any pending audio to be sent. Call
When the connection is no longer needed, call
Prerequisites
- Select a model that supports AOQ. For specific support details, refer to Model and Protocol Support Scope.
- Configure and call the API Key on the business AppServer, matching the region and workspace.
- Import the platform-specific AOQ SDK by downloading fromSDK. The following explanation uses Android Java and Opus transmission as an example; please also import the Opus plugin. When using a microphone for audio capture, request recording permissions on the client side.
1. Obtain Connection Credentials
Set the environment variable DASHSCOPE_API_KEY on the AppServer, replacing {endpoint} with the endpoint for the selected region and workspace. Fun-ASR-Realtime uses the Inference connection path; the following example uses fun-asr-realtime.
WorkspaceId in the access domain name is fixed as token-plan. The clientIp in the request body is optional; enter the client’s real public IP address so that the gateway can select a Relay access point. If left blank, the gateway uses the request source IP.
The API Key is stored only on the AppServer. The AppServer returns the connection parameters in the table below to the client; the sidExpiresInSecs in the response indicates the session validity period, in seconds.
| Gateway Response Fields | AoqConnectConfig Field |
|---|---|
aoqTokenForClient | token |
sid | sid |
clientRelayCertFingerprint | certFingerprint |
clientRelayEndpoints | relayEndpoints, convert endpoint, port, and route_index item by item |
extraInfo.workspaceIdHash | workspaceIdHash |
2. Initialize SDK and Connect
Use AoqCreateConfig to set the SDK working directory, call AoqClientEngine.createEngine to create the engine and register callbacks. Handle connection status in onConnectionStatusChange, parse model events in onDataMsg, and handle SDK errors in onError. Android SDK provides complete interface documentation for engine initialization and connection fields.
After filling the credential returned by the AppServer into connectConfig, publish the Audio and Data tracks and subscribe to the Data track. The following example configures 16 kHz mono Opus encoding and built-in capture, and disables Audio track transmission during connection establishment. The model’s input format is specified by the task event, and the SDK encoding configuration is used for media track transmission.
onConnectionStatusChange to return AoqConnectionStatusConnected before starting the task. In case of connection exceptions, clear the local task state; re-establishing the connection requires obtaining new connection credentials.
3. Start the recognition task and handle events
Serialize the following JSON into UTF-8, write it into AoqDataMsg.data, and then send it via the Data track using sendDataMsg. Each task generates a new task_id; all events in this round use the same value.
header.event from onDataMsg. Uplink audio is allowed only after receiving task-started.
payload.output.sentence in result-generated, and skip the heartbeat of heartbeat=true. sentence_end=false represents intermediate text, while sentence_end=true represents the final sentence text. Update the text for the same sentence according to sentence_id. Upon receiving task-failed, stop this round of uplink and read header.error_code and header.error_message.
For task parameters and event fields, refer to Client Events and Server Events.
4. End the Task and Release Resources
After recording ends, stop capturing new audio and wait for any pending audio to be sent. Call enableSendMediaStream to close the Audio track uplink, then send finish-task via the Data track. Keep the connection open to receive remaining recognition results and await task-finished.
stopAudioCapture, disconnect, and AoqClientEngine.destroy in sequence to release resources.