flutter_tts_improved 1.0.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 85

Flutter Text To Speech Improved #

The flutter_tts plugin (https://github.com/dlutton/flutter_tts) is GREAT! Thanks @dlutton for all your hard work on getting it up and running. Now it is time to improve it by allowing for the Utterances API to report the 'progress' of the speech of the Utterance. This is relatively new for Android, only introduced in Android 26. It has been around on iOS for quite some time though, back around iOS 7.0.

My goal with this plugin is to allow everyone in flutter to track where the speech is currently at. This can be used to print words on the screen as they are spoken, highlight words of a paragraph as they are uttered, for timing how long each word takes to say at a given speed, or really any other weird reason you may need it. This API is powerful, and I know people are asking for it, and there is simply not a plugin yet that covers it. Surprise! Now there is. You are welcome.

Features #

  • [x] Android & iOS
    • [x] speak
    • [x] stop
    • [x] get languages
    • [x] set language
    • [x] is language available
    • [x] set speech rate
    • [x] set speech volume
    • [x] set speech pitch
    • [x] get voices
    • [x] set voice
    • [x] #NEW track what word is currently being said (on all iOS 7+ and Android 26+)
  • [x] Android
    • [x] set Silence

Usage #

Android #

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

minSdkVersion 21

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_improved:
  • instantiate a FlutterTtsImproved instance
FlutterTtsImproved tts = new FlutterTtsImproved();

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

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

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

List<dynamic> languages = await tts.getLanguages;

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

await tts.setSpeechRate(1.0);

await tts.setVolume(1.0);

await tts.setPitch(1.0);

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

Listening for platform calls #

// #NEW
tts.setProgressHandler((String fullPhrase, int wordOffset, int endOfWordOffset, String currentWord) {
  setState(() {
    _wordToDisplay = currentWord;
  });
});

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

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

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

Getting Started #

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

For help on editing plugin code, view the documentation.

1.0.3 #

  • Formatted code according to dart pub specs

1.0.2 #

  • Fully tested on multiple iOS and Android devices and emulators

0.0.2 #

  • Fixed issue with iOS config that prevented plugin from working on iOS devices

0.0.1 #

  • Added the usage of the built-in Android and iOS Utterance APIs, when available
  • Added stub for older android APIs when Utterance API is not available (pre-android-26)

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_tts_improved/flutter_tts_improved.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  FlutterTtsImproved tts = FlutterTtsImproved();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    print('VOICES: ${await tts.getVoices}');
    print('LANGUAGES: ${await tts.getLanguages}');

    tts.setProgressHandler((String words, int start, int end, String word) {
      setState(() {
        _platformVersion = word;
      });
      print('PROGRESS: $word => $start - $end');
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.only(bottom: 20),
                child: RaisedButton(
                  onPressed: () {
                    tts.speak('say something that we can debug please');
                  },
                  child: Text('Say Something...'),
                ),
              ),
              Text('Running on: $_platformVersion\n'),
            ],
          ),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_tts_improved: ^1.0.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_tts_improved/flutter_tts_improved.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
71
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
85
Learn more about scoring.

We analyzed this package on Apr 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test