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.
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 example JSON as a string to the Dart method.
Set each task parameter using
For Sambert,
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 Sambert 单向流式语音合成 and run the example.
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().
-
Subscribe to
ttsEventStreamandttsAudioStreambefore calling these methods. -
Use the connection JSON below as
initJson. Pass the task parameters tottsSetParamas key-value pairs.After initialization, set the task parameters one at a time. CallttsStartwith the complete text and read the returned audio from the stream. -
TTS_EVENT_ENDindicates the SDK has returned its audio. CallttsReleasewhen finished.
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 example JSON as a string to the Dart method.
Task parameters
Set each task parameter using ttsSetParam(key, value). Pass numeric values as strings, as shown.
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 signature | Purpose |
|---|---|
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.
| Event | Purpose |
|---|---|
TTS_EVENT_START | Synthesis or response audio started |
TTS_EVENT_END | Task or current audio output complete |
TTS_EVENT_CANCEL | Task canceled |
TTS_EVENT_ERROR | Task failed |