Flutter text to speech App Tutorial | flutter tts

Fetching Comments ...

Flutter text to speech App Tutorial | flutter tts

A flutter text to speech plugin which is built in Swift for iOS and Java for Android.

It’s Fully compatible on:

  • Android
  • iOS
  • macOS
  • Web

So if you are planning or developing flutter text to speech app then this will helpful for you.

Features

  • Android, iOS, Web, & macOS
    • speak
    • stop
    • get languages
    • set language
    • set speech rate
    • set speech volume
    • set speech pitch
    • is language available
  • Android, iOS
    • get voices
  • set voice
    • speech marks (requires iOS 7+ and Android 26+)
    • synthesize to file (requires iOS 13+)
  • iOS, Web
    • pause
  • Android
    • set Silence
  • iOS
    • set shared instance
    • set audio session category

Implementation:

  • add the dependency to your pubspec.yaml file.
dependencies:
  flutter:
    sdk: flutter
  flutter_tts:
  • instantiate FlutterTts
final flutterTts = FlutterTts();
  • To set shared audio instance (iOS only):
await flutterTts.setSharedInstance(true);
await flutterTts
        .setIosAudioCategory(IosTextToSpeechAudioCategory.playAndRecord, [
      IosTextToSpeechAudioCategoryOptions.allowBluetooth,
      IosTextToSpeechAudioCategoryOptions.allowBluetoothA2DP,
      IosTextToSpeechAudioCategoryOptions.mixWithOthers
    ]);
  • After configuring above things you are ready to go, Now simply call speak() function and pass your word and it will speak your word. To stop flutter tts you can call stop() function from flutterTts instance and it will stop speaking.

Few optional things to do:

If you aim is not only speaking word on button click and stop speaking on button click. Then you can use following functions according to your need.

For example

getLanguages(), setLanguage(), setSpeechRate(), setVolume(), setPitch(), isLanguageAvailable(), setSharedInstance()

getLanguages

getLanguages() is used to get available supported languages on device for flutter text to speech plugin, it Returns a list of available languages.

setLanguage

setLanguage() is used to set language, for ex. await flutterTts.setLanguage("en-US");

and other functions

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 and Web only
await flutterTts.pause();

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

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

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

await flutterTts.getEngines();

Few callbacks which are provided from platform

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;
  });
});

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

flutterTts.setContinueHandler((msg) {
  setState(() {
    ttsState = TtsState.continued;
  });
});
If you wan’t me to create dummy flutter text to speech app then comment below. I’ll make demo app and share source code on Github.

LearnPainLess On Instagram

Get The Best Of All Hands Delivered To Your Inbox

Subscribe to our newsletter and stay updated.