All Products
Search
Document Center

Alibaba Cloud Model Studio:Sambert Flutter SDK

Last Updated:Nov 19, 2026

Integrate Sambert with the Flutter SDK. See the Dart call sequence, request parameters, and events.

Sambert uses unidirectional streaming. Your app sends the complete text and receives synthesized audio in chunks.

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.

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

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.

dependencies:
  aliyun_neonui:
    path: packages/aliyun_neonui

Run flutter pub get, then import the SDK in your Dart file.

import 'package:aliyun_neonui/neonui.dart';

Quick start

Run the example app

  1. Connect your target device. Run these commands from the extracted SDK directory.

    cd example
    flutter pub get
    flutter run
    
  2. 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.

  3. Open Sambert 单向流式语音合成 and run the example.

The complete example is in example/lib/pages/dashscope/dash_sambert_tts_page.dart in the SDK package.

Models in the included example

  • sambert-zhinan-v1

Call procedure

Call these methods on the instance returned by NativeNui().

  1. Subscribe to ttsEventStream and ttsAudioStream before calling these methods.

  2. Use the connection JSON below as initJson. Pass the task parameters to ttsSetParam as key-value pairs.

    await nui.ttsInitialize(params: initJson);
    for (final entry in taskParams.entries) {
      await nui.ttsSetParam(entry.key, entry.value);
    }
    await nui.ttsStart(priority: '1', taskId: '', text: '你好,欢迎使用语音合成。');
    

    After initialization, set the task parameters one at a time. Call ttsStart with the complete text and read the returned audio from the stream.

  3. TTS_EVENT_END indicates the SDK has returned its audio. Call ttsRelease when finished.

Request parameters

The following example uses a model from the package. For additional settings, see example/lib/dashscope_params.dart in the package.

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 example JSON as a string to the Dart method.

{
  "url": "wss://YOUR_WS_HOST/api-ws/v1/inference",
  "device_id": "YOUR_DEVICE_ID",
  "mode_type": "2"
}

Task parameters

Set each task parameter using ttsSetParam(key, value). Pass numeric values as strings, as shown.

{
  "apikey": "YOUR_API_KEY",
  "model": "sambert-zhinan-v1",
  "format": "pcm",
  "enable_audio_decoder": "0",
  "sample_rate": "48000",
  "volume": "50",
  "rate": "1.0",
  "pitch": "1.0"
}

For Sambert, model selects the model ID associated with the voice. This example matches the 48 kHz voice in the package. Play audio at the same sample rate.

API reference

Methods

For methods returning Future<int>, result code 0 indicates success.

Method signaturePurpose
Future<int> ttsInitialize({required String params, int logLevel = 0, bool saveLog = false})Initialize the SDK with connection parameters as a JSON string.
Future<int> ttsSetParam(String key, String value)Set one string-valued task parameter.
Future<int> ttsStart({required String priority, required String taskId, required String text})Submit the complete text. The example uses priority: '1' and taskId: ''.
Future<int> ttsPause()Pause synthesis.
Future<int> ttsResume()Resume synthesis.
Future<int> ttsCancel(String taskId)Cancel synthesis by task ID.
Future<String?> ttsGetParam(String key)Read a parameter. Use error_msg for error details.
Future<int> ttsRelease()Release SDK resources.

Events and audio streams

Read events from ttsEventStream. For events delivered as Dart maps, read the event name from event and use resultCode to inspect task errors.

The ttsAudioStream stream emits audio chunks for the app to play.

EventPurpose
TTS_EVENT_STARTSynthesis or response audio started
TTS_EVENT_ENDTask or current audio output complete
TTS_EVENT_CANCELTask canceled
TTS_EVENT_ERRORTask failed