Use the Flutter SDK for CosyVoice bidirectional streaming speech synthesis, including the Dart call sequence, request parameters, and events.
Supported regions: Singapore and China North 2 (Beijing). Use an API key and endpoint from the region you call; see Regions and endpoints.
CosyVoice uses a bidirectional streaming interface. Your app can send text in chunks and receive audio while synthesis is in progress.
Download the mobile speech SDK package from SDK downloads. After extracting it, copy the Flutter SDK directory into
Run
Get an instance with
The following example uses one model from the package. For other fields, see
The following example uses Singapore. For another region, replace
Pass this JSON as a string to the Dart methods.
For methods that return
Read events from
Prerequisites
- Set up Flutter development for your target platform. The example project requires Dart 3.4 or later, below 4.0.
- Obtain an API key.
Install the SDK
Download the mobile speech SDK package from SDK downloads. After extracting it, copy the Flutter SDK directory into packages/aliyun_neonui in your project and add a local dependency in pubspec.yaml.
flutter pub get, then import the SDK in your Dart files.
Quick start
- Connect your target device and run the example from the extracted SDK directory.
- Open the example app, select a service endpoint, and enter your API key.
- Open the CosyVoice bidirectional streaming synthesis example and run it.
example/lib/pages/dashscope/dash_cosy_voice_stream_tts_page.dart in the package.
The package includes the following models:
cosyvoice-v3-flashcosyvoice-v3-plus
Call sequence
Get an instance with NativeNui() and call the following methods.
- Subscribe to
streamTtsEventStreamandstreamTtsAudioStreamfirst, then start synthesis.ticketJsonandparamsJsoncorrespond to the connection parameters and task parameters in Request parameters.
- After synthesis starts, send text in chunks and play the audio received from the audio stream.
- End text input with
streamTtsStop. After the synthesis complete event arrives, callstreamTtsRelease.
Request parameters
The following example uses one model from the package. For other fields, see example/lib/dashscope_params.dart in the package. For more parameter details, see Android SDK, iOS SDK, and HarmonyOS SDK.
Connection parameters
The following example uses Singapore. For another region, replace ap-southeast-1 with the region ID. See Regions and endpoints. Pass this JSON as the ticket parameter.
Task parameters
Pass this JSON as a string to the Dart methods.
model and voice must match. format and sample_rate define the output audio format and sample rate. The example enables enable_audio_decoder when you select MP3 or Opus.
API reference
Methods
For methods that return Future<int>, a result code of 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 with the ticket and params JSON strings. |
Future<int> streamTtsSend(String text) | Send another chunk of text. |
Future<int> streamTtsStop({bool flagAsync = true}) | End text input. flagAsync defaults to true, which stops asynchronously. |
Future<int> streamTtsAsyncPlay({required String ticket, required String params, required String text, String sessionId = "", int logLevel = 0, bool saveLog = false}) | Submit the complete text at once and receive the synthesis result asynchronously. |
Future<int> streamTtsCancel() | Cancel the current session or synthesis. |
Future<int> streamTtsRelease() | Release SDK resources. |
Events and audio streams
Read events from streamTtsEventStream. For Dart Map events, use event to identify the event name, and use resultCode on task events to check for errors.
The streamTtsAudioStream audio stream returns audio chunks. Your app is responsible for playback.
| Event | Purpose |
|---|---|
STREAM_INPUT_TTS_EVENT_SYNTHESIS_STARTED | Synthesis or reply audio started |
STREAM_INPUT_TTS_EVENT_SENTENCE_SYNTHESIS | Sentence-level synthesis event |
STREAM_INPUT_TTS_EVENT_SYNTHESIS_COMPLETE | Task or current audio output completed |
STREAM_INPUT_TTS_EVENT_TASK_FAILED | Task failed |