This section describes the authentication, media configuration, connection establishment, and task-event handling processes for accessing Qwen-Audio-TTS via AOQ.
When accessing Qwen-Audio-TTS through AOQ, the client submits text and task events via the Data track and receives synthesized speech via the Audio track.
Set the environment variable
If using Token Plan, the
Use
Wait for
Serialize the following JSON into UTF-8, write it to
Read
SDK receives and plays the speech from the Audio track.
After submitting all text, send
When no longer using the connection, wait for any received audio to finish playing, then call
Prerequisites
- Select a model that supports AOQ. Protocol support for
qwen-audio-3.1-tts-plus,qwen-audio-3.1-tts-flash,qwen-audio-3.0-tts-plus, andqwen-audio-3.0-tts-flashis outlined in 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 from SDK Download. The following examples use Android Java and Opus transmission; please also import the Opus plugin. Text-to-speech synthesis does not require microphone or camera permissions.
1. Obtain Connection Credentials
Set the environment variable DASHSCOPE_API_KEY on the AppServer, replacing {endpoint} with the access domain name for the selected region and workspace. Qwen-Audio-TTS uses the Inference connection path; the following example uses qwen-audio-3.0-tts-flash.
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 credentials returned by the AppServer into connectConfig, publish the Data track and subscribe to the Audio and Data tracks. The example configures 24 kHz mono Opus decoding and starts the player. The model’s output format is specified by the task event, and the SDK decoding 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 synthesis task and process events
Serialize the following JSON into UTF-8, write it to AoqDataMsg.data, and then send it via the Data track using sendDataMsg. Each task generates a new task_id; all events in this round share the same value. Use the voice that matches the selected model.
header.event in onDataMsg. Upon receiving task-started, send one or more continue-task to submit the text.
result-generated represents the task event on the Data track, while the audio is transmitted via the Audio track. Upon receiving task-failed, read header.error_code and header.error_message, and end the current task.
Task parameters and event fields are detailed in Client Events and Server Events.
4. End the Task and Release Resources
After submitting all text, send finish-task. Keep the connection open and wait for the server to synthesize the remaining text and return task-finished.
stopAudioPlayer, disconnect, and AoqClientEngine.destroy in sequence to release resources.