Use AOQ to connect to fun-asr-realtime, stream microphone audio, and receive transcription results in real time. The client code uses Android Java, and other AOQ-supported platforms provide the same interfaces.
Solution overview
fun-asr-realtime transcribes an audio stream into punctuated text in real time. The AOQ SDK transports media and events over separate tracks: the client sends audio over the Audio track and uses the Data track to send control events and receive recognition events. The model uses the Inference event protocol, not the Realtime event protocol.
This solution is suitable for live captions, meeting transcription, voice input, and intelligent assistants. The Audio track eliminates the need to encode audio in event messages, while the Data track preserves task semantics such as run-task, result-generated, and finish-task.
- The client requests temporary AOQ connection credentials from the application server.
- The application server uses an API key to request a token from Model Studio and returns the connection fields to the client.
- The client establishes an AOQ connection and sends run-task. After task-started is received, the client starts sending microphone audio.
- The server continuously returns result-generated events. After the client sends finish-task, it waits for the final result and task-finished.
Prerequisites
- Enable Alibaba Cloud Model Studio and create an API key as described in Obtain and configure an API key. Store the API key only on the application server. Do not include it in client code or commit it to a code repository.
- Select an AOQ endpoint for the region in which your application is deployed. For endpoint selection, see Select a region, deployment scope, and endpoint.
- Download the latest AOQ Client SDK as described in SDK download. This tutorial transports PCM audio and does not require the optional Opus plugin.
- Set up an application server as described in Token authentication and implement server-side proxy authentication for the AOQ Inference protocol. Before each new connection, the client must obtain new connection credentials from the application server.
Import the SDK
Select the SDK integration instructions for your development platform. The client implementation uses Android Java; other platforms follow the same API design and event flow.
- Android
- iOS
- HarmonyOS
- Linux (Python)
- Place AoqClientSdk-release.aar in app/libs and configure the dependency and ABIs in app/build.gradle:
- Declare network and recording permissions in AndroidManifest.xml:
- Request the RECORD_AUDIO runtime permission before recording starts. Speech recognition does not require the CAMERA permission.
Try the demo
Use the Android demo from Alibaba Cloud Model Studio to quickly verify AOQ connectivity. Download the APK and configure the API key and workspaceId to try selected models.
Scan the following QR code to download the demo:

Implementation flow
- The application server uses the Inference token URL to obtain AOQ connection parameters for fun-asr-realtime.
- The client maps the token response to AoqConnectConfig, publishes the Audio and Data tracks, and subscribes to the Data track.
- The client configures audio encoding parameters based on business and model requirements, starts microphone capture with audio sending disabled, and establishes the AOQ connection.
- After the connection is established, the client sends run-task and enables Audio-track sending after task-started is received.
- The client handles result-generated in onDataMsg. To stop recognition, it first disables Audio-track sending and then sends finish-task.
- After task-finished is received, the client can start another task on the same connection with a new task_id, or disconnect and destroy the engine.

Obtain a token on the application server
Set DASHSCOPE_API_KEY on the application server and send a request to the endpoint for the selected region. clientIp is the actual public IP address of the client device. The field is optional, but we recommend that you provide it so that the service can select an appropriate relay endpoint.
Response field | SDK field |
aoqTokenForClient | AoqConnectConfig.token |
sid | AoqConnectConfig.sid |
clientRelayCertFingerprint | AoqConnectConfig.certFingerprint |
clientRelayEndpoints | AoqConnectConfig.relayEndpoints |
extraInfo.workspaceIdHash | AoqConnectConfig.workspaceIdHash |
Implement the Android client
The following steps divide the Android Java client code according to the actual connection and task sequence. Each snippet is taken from the complete example later in this topic.
1. Create the engine and register callbacks
Create an AOQ client engine and register callbacks for connection state changes and Data-track events. Implement the callback logic for your application, and start recognition only after the connection is established.
2. Configure audio encoding
Configure the audio encoding sent to the model. Set the format, sample rate, and channel count based on your application and model requirements. The following code uses 16 kHz mono PCM as an example. For supported values, see the run-task parameters in Client events.
3. Configure the connection and transport tracks
Configure the AOQ connection with the credentials returned by the application server, and select the tracks to publish and subscribe to based on your application. For real-time speech recognition, this example publishes Audio and Data tracks and subscribes to the Data track.
4. Start audio capture and establish the connection
Configure audio capture and establish the AOQ connection. Choose built-in or external capture, whether to enable VoIP mode, and the channel count based on your application. Keep Audio-track sending disabled until task-started is received.
5. Start a recognition task
After the connection is established, generate a task ID and send run-task to start recognition. Configure model, format, sample_rate, and other task parameters for the model and audio input that you use. For details, see Client events.
6. Handle server events
Handle task states, recognition results, and errors, and pass the results to your application. Implement callbacks based on your UI and state-management requirements. Send audio only after task-started is received, and exclude heartbeat events from displayed transcripts. For the complete response schema, see Server events.
7. Finish the recognition task
When the user finishes the current recording, stop sending audio and send finish-task. Keep the connection open until the final transcript and task-finished arrive. You can then start another task or release the connection based on your application flow. For the event format, see Client events.
8. Disconnect and destroy the engine
Release audio capture, the AOQ connection, and engine resources when the page is destroyed or recognition is no longer needed. Choose the release point based on your application lifecycle, and do not release resources immediately after finish-task is sent.
Complete example
This Android Java class maps the JSON response from the application server to AoqConnectConfig and combines the preceding connection, capture, task, and resource-release logic.
Usage example
Pass the token response from the application server to parseConnectConfig and create the client. The first recognition task starts automatically after the connection is established. The stop button ends only the current task. Release the connection and local resources when the page is destroyed.
Run and verify
- Start the application server. Make sure that the token request returns HTTP 200 and includes sid, aoqTokenForClient, clientRelayEndpoints, clientRelayCertFingerprint, and extraInfo.workspaceIdHash.
- Install and run the app on an Android device, grant microphone access, and speak.
- Observe the callbacks. A normal event sequence is:
Common scenarios
Multiple recognition tasks over one connection
After task-finished is received, call beginRecognition to start another task over the same AOQ connection. Each task requires a new task_id. You do not need to request another token or reconnect while the connection remains active. If the connection is closed, obtain new connection credentials.
Background recognition on Android
On Android 10 or later, use a foreground service with foregroundServiceType=microphone to continue microphone capture while the app is in the background. Start the service while the app is still visible to the user.
Troubleshooting
Issue | Solution |
The connection fails | Make sure that the token is valid, the endpoint matches the application region, and the application server passes the actual public IP address of the client device. Do not reuse a token after the connection is closed. |
The task starts but no transcript is returned | Enable Audio-track sending only after task-started is received. Check the input format, sample rate, and other parameters against Client events for the model. |
The final transcript is not returned | Disable Audio-track sending before sending finish-task. Wait for the final result-generated event and task-finished instead of disconnecting immediately. |
The Android SDK fails to load | Make sure that the AAR is included as a dependency and that the app packages an SDK-supported ABI: armeabi-v7a or arm64-v8a. |
A subsequent task over the same connection is rejected | Make sure that task-finished was received for the previous task and generate a new task_id for the next run-task event. |