Skip to main content
SDK access

CosyVoice Flutter SDK

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.

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.
Do not hardcode a long-lived API key in a mobile app. For models that accept temporary API keys, have your application server generate a temporary API key and send it to the client.

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.
dependencies:
  aliyun_neonui:
    path: packages/aliyun_neonui
Run flutter pub get, then import the SDK in your Dart files.
import 'package:aliyun_neonui/neonui.dart';

Quick start

  1. Connect your target device and run the example from the extracted SDK directory.
cd example
flutter pub get
flutter run
  1. Open the example app, select a service endpoint, and enter your API key.
  2. Open the CosyVoice bidirectional streaming synthesis example and run it.
The complete example is in example/lib/pages/dashscope/dash_cosy_voice_stream_tts_page.dart in the package. The package includes the following models:
  • cosyvoice-v3-flash
  • cosyvoice-v3-plus

Call sequence

Get an instance with NativeNui() and call the following methods.
  1. Subscribe to streamTtsEventStream and streamTtsAudioStream first, then start synthesis. ticketJson and paramsJson correspond to the connection parameters and task parameters in Request parameters.
await nui.streamTtsStart(ticket: ticketJson, params: paramsJson);
await nui.streamTtsSend('Hello, ');
await nui.streamTtsSend('welcome to speech synthesis.');
  1. After synthesis starts, send text in chunks and play the audio received from the audio stream.
  2. End text input with streamTtsStop. After the synthesis complete event arrives, call streamTtsRelease.

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.
{
  "apikey": "YOUR_API_KEY",
  "device_id": "YOUR_DEVICE_ID",
  "url": "wss://ap-southeast-1.maas.qwencloudapi.com/api-ws/v1/inference"
}

Task parameters

Pass this JSON as a string to the Dart methods.
{
  "model": "cosyvoice-v3-flash",
  "voice": "longanhuan_v3",
  "format": "pcm",
  "enable_audio_decoder": false,
  "sample_rate": 24000,
  "volume": 50,
  "rate": 1.0,
  "pitch": 1.0
}
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 signaturePurpose
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.
EventPurpose
STREAM_INPUT_TTS_EVENT_SYNTHESIS_STARTEDSynthesis or reply audio started
STREAM_INPUT_TTS_EVENT_SENTENCE_SYNTHESISSentence-level synthesis event
STREAM_INPUT_TTS_EVENT_SYNTHESIS_COMPLETETask or current audio output completed
STREAM_INPUT_TTS_EVENT_TASK_FAILEDTask failed