Flutter Text To Speech

Today we are taking a look at flutter plugin text to speech

0
86

Target Audience: Beginner

Plugin: Text-To-Speech Flutter plugin: flutter_tts

This flutter_tts plugin used to interact with native functionality. Under the hood, it uses TextToSpeech for Android, and AVSpeechSynthesizer for IOS platform. In this, we are exploring the methods of flutter_tts plugin. To check what we can achieve by this plugin.

Features

Android, iOS, & Web

  • [x] speak
  • [x] stop
  • [x] get languages
  • [x] set language
  • [x] set speech rate
  • [x] set speech volume
  • [x] set speech pitch
  • [x] is language available

Let’s start by installing it.pubspec.yaml dependencies

dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
flutter_tts: 1.0.0

As it is mentioned by officials

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

minSdkVersion 21

There are three handlers (setStartHandler ,setCompletionHandler,setErrorHandler) which are responsible for changing the state of play, stop and for the error handler. We have to initialize these handlers at first before using its state (TtsState)

flutterTts = FlutterTts();
flutterTts.setStartHandler(() {
setState(() {
print("playing");
ttsState = TtsState.playing;
});
});
flutterTts.setCompletionHandler(() {
setState(() {
print("Complete");
ttsState = TtsState.stopped;
});
});
flutterTts.setErrorHandler((msg) {
setState(() {
print("error: $msg");
ttsState = TtsState.stopped;
});
});

To play

Our input is in our speak method. if the input is valid then we change the state to TtsState.playing

var result = await flutterTts.speak("I am a flutter developer");
if (result == 1) setState(() => ttsState = TtsState.playing);

To Stop

var result = await flutterTts.stop();
if (result == 1) setState(() => ttsState = TtsState.stopped);

Language change

flutterTts.setLanguage("en-Us");

you can get the list of languages it’s currently supported

languages = await flutterTts.getLanguages;

Setting Voice

_flutterTts.setVoice("en-us-x-sfg#male_1-local" )

You Can Check Language Availability

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

You can change the volume, pitch rate

await flutterTts.setVolume(volume);
await flutterTts.setSpeechRate(rate);
await flutterTts.setPitch(pitch);

Link for repository:

flutter-devs/text-To-speech-demo
A new Flutter text to speech application. This project is a starting point for a Flutter application. A few resources…github.com

Thanks for reading this article if you find anything that could be improved please let me know, I would love to improve.


Feel free to connect with us:
And read more articles from FlutterDevs.com

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on Facebook, GitHub, Twitter and linkedIn for any flutter related queries.

We welcome feedback, and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences!.


LEAVE A REPLY

Please enter your comment!
Please enter your name here