Integrate Qwen-Audio-TTS / CosyVoice with the Flutter SDK. See the Dart call sequence, request parameters, and events.
Qwen-Audio-TTS and CosyVoice share bidirectional streaming APIs. Your app can send text in chunks while receiving synthesized audio.
Open Mobile speech SDK selection and download, select Flutter, and download and extract the package.
Copy the extracted SDK directory to
Run
Call these methods on the instance returned by
The following example uses a model from the package. For additional settings, see
Pass the connection JSON as
Pass the example JSON as a string to the Dart method.
Use a
For methods returning
Read events from
Prerequisites
- Set up Flutter development for your target platform. The example app requires Dart 3.4 or later, below 4.0.
- Get an API key. Choose a service endpoint in the same region as the API key. If you use a workspace-specific domain, also get your workspace ID.
Install the SDK
Open Mobile speech SDK selection and download, select Flutter, and download and extract the package.
Copy the extracted SDK directory to packages/aliyun_neonui in your Flutter project and add the local dependency to pubspec.yaml.
flutter pub get, then import the SDK in your Dart file.
Quick start
Run the example app
-
Connect your target device. Run these commands from the extracted SDK directory.
- In the example app, open 百炼(DashScope) from the home screen. Select a service endpoint and enter your API Key. If you use a workspace-specific domain, enter your WorkspaceId as well.
- Open Qwen-Audio-TTS/CosyVoice 双向流式语音合成 and run the example.
example/lib/pages/dashscope/dash_cosy_voice_stream_tts_page.dart in the SDK package.
Models in the included example
qwen-audio-3.1-tts-flashqwen-audio-3.1-tts-plusqwen-audio-3.0-tts-flashqwen-audio-3.0-tts-pluscosyvoice-v3-flashcosyvoice-v3-plus
Call procedure
Call these methods on the instance returned by NativeNui().
-
Subscribe to
streamTtsEventStreamandstreamTtsAudioStreambefore calling these methods. -
Use the connection JSON and task JSON below as
ticketJsonandparamsJson.After starting synthesis, send text in chunks and play the audio received from the stream. -
End text input with
streamTtsStop. After the synthesis completion event, callstreamTtsRelease.
Request parameters
The following example uses a model from the package. For additional settings, see example/lib/dashscope_params.dart in the package. For additional parameter details, see Android SDK, iOS SDK, HarmonyOS SDK.
Replace YOUR_WS_HOST with the WebSocket host for your region, YOUR_DEVICE_ID with a device identifier, and YOUR_API_KEY with an API key for that region.
Connection parameters
Pass the connection JSON as ticket.
Task parameters
Pass the example JSON as a string to the Dart method.
voice compatible with the model. format and sample_rate set the output format and sample rate. The example enables enable_audio_decoder for MP3 or Opus.
API reference
Methods
For methods returning Future<int>, result code 0 indicates success.
| Method signature | Purpose |
|---|---|
Future<int> streamTtsStart({required String ticket, required String params, String sessionId = "", int logLevel = 0, bool saveLog = false}) | Create a streaming synthesis session. Pass ticket and params as two JSON strings. |
Future<int> streamTtsSend(String text) | Send the next text chunk. |
Future<int> streamTtsStop({bool flagAsync = true}) | End text input. flagAsync defaults to true for asynchronous stopping. |
Future<int> streamTtsAsyncPlay({required String ticket, required String params, required String text, String sessionId = "", int logLevel = 0, bool saveLog = false}) | Submit the full text in one call and receive synthesis results asynchronously. |
Future<int> streamTtsCancel() | Cancel the current session or synthesis task. |
Future<int> streamTtsRelease() | Release SDK resources. |
Events and audio streams
Read events from streamTtsEventStream. For events delivered as Dart maps, read the event name from event and use resultCode to inspect task errors.
The streamTtsAudioStream stream emits audio chunks for the app to play.
| Event | Purpose |
|---|---|
STREAM_INPUT_TTS_EVENT_SYNTHESIS_STARTED | Synthesis or response audio started |
STREAM_INPUT_TTS_EVENT_SENTENCE_SYNTHESIS | Sentence-level synthesis event |
STREAM_INPUT_TTS_EVENT_SYNTHESIS_COMPLETE | Task or current audio output complete |
STREAM_INPUT_TTS_EVENT_TASK_FAILED | Task failed |