Masamune Text-to-Speech
[GitHub](https://github.com/mathrunet) | [YouTube](https://www.youtube.com/c/mathrunetchannel) | [Packages](https://pub.dev/publishers/mathru.net/packages) | [X](https://x.com/mathru) | [LinkedIn](https://www.linkedin.com/in/mathrunet/) | [mathru.net](https://mathru.net)
Masamune Text-to-Speech
Usage
Installation
Add the package to your project.
flutter pub add masamune_text_to_speech
Run flutter pub get when editing pubspec.yaml manually.
Register the Adapter
Configure TextToSpeechMasamuneAdapter before runApp. Set default language, speech rate, or iOS audio category if needed.
// lib/adapter.dart
/// Masamune adapters used in the application.
final masamuneAdapters = <MasamuneAdapter>[
const UniversalMasamuneAdapter(),
const TextToSpeechMasamuneAdapter(
defaultLocale: Locale('en', 'US'), // Default language (required)
defaultSpeechRate: 0.5, // Speech speed (0.0-1.0, default: 1.0)
defaultVolume: 1.0, // Volume (0.0-1.0, default: 1.0)
defaultPitch: 1.0, // Pitch (0.0-1.0, default: 1.0)
defaultIosAudioCategory: TextToSpeechIosAudioCategory.playback, // iOS audio category
defaultIosAudioCategoryOptions: [
TextToSpeechIosAudioCategoryOptions.mixWithOthers, // Mix with other audio
],
),
];
Text-to-Speech Controller
Use TextToSpeechController to speak text, stop playback, and query voices.
class TextReaderPage extends PageScopedWidget {
@override
Widget build(BuildContext context, PageRef ref) {
final tts = ref.page.controller(TextToSpeechController.query());
// Initialize on page load
ref.page.on(
initOrUpdate: () {
tts.initialize();
},
);
return Scaffold(
appBar: AppBar(title: const Text("Text to Speech")),
body: Column(
children: [
TextField(
onChanged: (text) {
// Store text to speak
},
),
ElevatedButton(
onPressed: () async {
await tts.speak("Hello Masamune!");
},
child: const Text("Speak"),
),
ElevatedButton(
onPressed: () async {
await tts.stop();
},
child: const Text("Stop"),
),
],
),
);
}
}
Configure Voice Parameters
Change pitch, rate, or language dynamically.
await tts.setLanguage("ja-JP");
await tts.setSpeechRate(0.6);
await tts.setPitch(1.2);
await tts.speak("こんにちは");
Use availableLanguages() and availableVoices() to present selection UI to users.
Queueing and Completion
await tts.speak()resolves when playback completes.- Use
tts.setQueueMode(QueueMode.queue)to enqueue multiple sentences. - Register
onCompletecallback to react when speech finishes.
Tips
- Set the iOS audio category (
playback,ambient, etc.) to control mixing with other audio. - Handle
tts.initialize()errors gracefully, especially on web where permissions differ. - Cache frequently used phrases if you need low latency in successive calls.
- Combine with
masamune_speech_to_textto build conversational interfaces.
GitHub Sponsors
Sponsors are always welcome. Thank you for your support!
Libraries
- masamune_text_to_speech
- Masamune plug-in adapter for use with Text-to-Speech. Provides a controller.