flutter_tts 3.6.2 copy "flutter_tts: ^3.6.2" to clipboard
flutter_tts: ^3.6.2 copied to clipboard

A flutter plugin for Text to Speech. This plugin is supported on iOS, Android, Web, & macOS.

Text To Speech #

pub package

A flutter text to speech plugin (Swift,Kotlin)

Features #

  • Android, iOS, Web, Windows & macOS
    • speak
    • stop
    • get languages
    • set language
    • set speech rate
    • set speech volume
    • set speech pitch
  • Android, iOS, Web & macOS
    • is language available
  • Android, iOS, Web, & Windows
    • get voices
    • set voice
  • Android, iOS
    • speech marks (requires iOS 7+ and Android 26+)
    • synthesize to file (requires iOS 13+)
  • Android, iOS, Web, & Windows
    • pause
  • Android
    • set silence
    • is language installed
    • are languages installed
    • get engines
    • set engine
    • get default engine
    • get default voice
    • set queue mode
    • get max speech input length
  • iOS
    • set shared instance
    • set audio session category

Usage #

macOS #

OSX version: 10.15

Example App from the macOS_app branch

Web #

Website from the example directory.

Android #

Change the minimum Android sdk version to 21 (or higher) in your android/app/build.gradle file.

minSdkVersion 21

Apps targeting Android 11 that use text-to-speech should declare TextToSpeech.Engine.INTENT_ACTION_TTS_SERVICE in the queries elements of their manifest.

Pausing on Android #

Android TTS does not support the pause function natively, so currently we use a work around. Your long text is split into sentences (Sentences are usually separated by punctuations like . : ; ? !) and when you call pause() the tts stops. When you call speak again, it does not continue at the exact point where it stopped but at the beginning of the exact sentence where the pause() function was called. For example: if the TTS was reading

The dog and cat jumped over the white fence. But there are two boxes beneath them. The car was red? However we got there. 

If you call pause() while the TTS was reading the sentence "The car was red?" It starts from the beginning of "The car was red? when you call .speak() again but NOT at the beginning of "The dog and cat jumped ...". Try it out :)

await flutterTts.pause()

iOS #

There's a known issue with integrating plugins that use Swift into a Flutter project created with the Objective-C template. Flutter#16049

Example

To use this plugin :

  dependencies:
    flutter:
      sdk: flutter
    flutter_tts:
  • instantiate FlutterTts
FlutterTts flutterTts = FlutterTts();

To set shared audio instance (iOS only):

await flutterTts.setSharedInstance(true);

To set audio category and options with optional mode (iOS only). The following setup allows background music and in-app audio session to continue simultaneously:

await flutterTts.setIosAudioCategory(IosTextToSpeechAudioCategory.ambient,
     [
          IosTextToSpeechAudioCategoryOptions.allowBluetooth,
          IosTextToSpeechAudioCategoryOptions.allowBluetoothA2DP,
          IosTextToSpeechAudioCategoryOptions.mixWithOthers
     ],
     IosTextToSpeechAudioMode.voicePrompt
);

To await speak completion.

await flutterTts.awaitSpeakCompletion(true);

To await synthesize to file completion.

await flutterTts.awaitSynthCompletion(true);

speak, stop, getLanguages, setLanguage, setSpeechRate, setVoice, setVolume, setPitch, isLanguageAvailable, setSharedInstance #

Future _speak() async{
    var result = await flutterTts.speak("Hello World");
    if (result == 1) setState(() => ttsState = TtsState.playing);
}

Future _stop() async{
    var result = await flutterTts.stop();
    if (result == 1) setState(() => ttsState = TtsState.stopped);
}

List<dynamic> languages = await flutterTts.getLanguages;

await flutterTts.setLanguage("en-US");

await flutterTts.setSpeechRate(1.0);

await flutterTts.setVolume(1.0);

await flutterTts.setPitch(1.0);

await flutterTts.isLanguageAvailable("en-US");

// iOS, Android and Web only
//see the "Pausing on Android" section for more info
await flutterTts.pause();

// iOS, macOS, and Android only
await flutterTts.synthesizeToFile("Hello World", Platform.isAndroid ? "tts.wav" : "tts.caf");

await flutterTts.setVoice({"name": "Karen", "locale": "en-AU"});

// iOS only
await flutterTts.setSharedInstance(true);

// Android only
await flutterTts.setSilence(2);

await flutterTts.getEngines;

await flutterTts.getDefaultVoice;

await flutterTts.isLanguageInstalled("en-AU");

await flutterTts.areLanguagesInstalled(["en-AU", "en-US"]);

await flutterTts.setQueueMode(1);

await flutterTts.getMaxSpeechInputLength;

Listening for platform calls #

flutterTts.setStartHandler(() {
  setState(() {
    ttsState = TtsState.playing;
  });
});

flutterTts.setCompletionHandler(() {
  setState(() {
    ttsState = TtsState.stopped;
  });
});

flutterTts.setProgressHandler((String text, int startOffset, int endOffset, String word) {
  setState(() {
    _currentWord = word;
  });
});

flutterTts.setErrorHandler((msg) {
  setState(() {
    ttsState = TtsState.stopped;
  });
});

flutterTts.setCancelHandler((msg) {
  setState(() {
    ttsState = TtsState.stopped;
  });
});

// Android, iOS and Web
flutterTts.setPauseHandler((msg) {
  setState(() {
    ttsState = TtsState.paused;
  });
});

flutterTts.setContinueHandler((msg) {
  setState(() {
    ttsState = TtsState.continued;
  });
});

// Android only
flutterTts.setInitHandler(() {
  setState(() {
    ttsState = TtsState.initialized;
  });
});

Getting Started #

For help getting started with Flutter, view our online documentation.

For help on editing plugin code, view the documentation.

1150
likes
0
pub points
99%
popularity

Publisher

verified publishertundralabs.com

A flutter plugin for Text to Speech. This plugin is supported on iOS, Android, Web, & macOS.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter, flutter_web_plugins

More

Packages that depend on flutter_tts