Learn Pain Less

HomeOur TeamContact
Flutter
Flutter text to speech App Tutorial | flutter tts
Pawneshwer Gupta
Pawneshwer Gupta
July 29, 2020
1 min
Flutter text to speech App Tutorial | flutter tts

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);
  • To set audio category and options (iOS only):
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.

Learn Flutter in 90 days with Pawneshwer!

Dart Beginners Course in Hindi free of cost for limited period.

Start Learning

250+

LESSONS

30+

COURSES

15+

TUTORS

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Tags

flutter ttsflutter text to speechflutter webgoogle flutterfeatured

Share


Pawneshwer Gupta

Pawneshwer Gupta

Software Developer

Pawneshwer Gupta works as a software engineer who is enthusiastic in creating efficient and innovative software solutions.

Expertise

Python
Flutter
Laravel
NodeJS

Social Media

Related Posts

Why we have to think about state in flutter (Beginner's question)
Why we have to think about state in flutter (Beginner's question)
August 08, 2020
2 min
Learn Pain Less  © 2024, All Rights Reserved.
Crafted with by Prolong Services

Quick Links

Advertise with usAbout UsContact Us

Social Media