4

I have an Android App that converts text to voice. each word/string on the array is a button that when selected it converts to voice. I am looking to implement this in Flutter.

private TextToSpeech tts; 

GridView grid;

String[] words = {

        "Flutter",
        "Dart",
        "React,
        "Java"
};


@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    tts =new TextToSpeech(this, this);
    setContentView(R.layout.activity_main);
    grid = (GridView) findViewById(R.id.grid);

Can anyone provide a solution in Dart/Flutter?

Thank you.

3 Answers 3

4

You can not use tts package as it is INCOMPATIBLE with Dart 2.0:

go for "flutter_tts " as it's working with Dart 2.0

https://pub.dartlang.org/packages/flutter_tts

     FlutterTts flutterTts = new FlutterTts();

     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");
Sign up to request clarification or add additional context in comments.

Comments

3

You may find the tts package for Flutter useful:

https://pub.dartlang.org/packages/tts

Here is the simple example

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

void main() {
  runApp(new Scaffold(
    body: new Center(
      child: new RaisedButton(
        onPressed: speak,
        child: new Text('Say Hello'),
      ),
    ),
  ));
}

speak() async {
  Tts.speak('Hello World');
}

While you may find a more in-depth example here:

https://pub.dartlang.org/packages/tts#-example-tab-

As for wiring this all together:

Can anyone provide a solution in Dart/Flutter?

Here is a simple example using a list to render buttons for each String in the list along with the onPressed actions to speak the words:

@override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("The App"),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: _buildWords(),
        ),
      ),
    );
  }

  List<String> words = ['hello', 'world', 'flutter', 'is', 'awesome'];

  List<Widget> _buildWords() {
    return words.map((String word) {
      return new RaisedButton(
        child: new Text(word),
        onPressed: () => Tts.speak(word),
      );
    }).toList();
  }

4 Comments

Thank you, did see that a few days ago, but still having issues creating the array and formating to convert each word to voice using OnPressed.
@DevEd I've updated the answer to include a snippet of how you can get this to work in Flutter.
Thanks a lot for following up. I have not been successful on running it. not sure if the problem is that I have to created a new widget or a children one. Can you paste the full code? Thanks again!
It worked!! It took me a few minutes to understand but its Finally Running and Working!! Cheers!!!
0

''' import 'package:flutter_tts/flutter_tts.dart';'''

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

''' you can adjust the time duration if your list is large'''

The function I used:

 Future.forEach (YOUR_LIST,(ITEM_IN_LIST) => 

 new Future.delayed(new Duration(seconds: 2), () {

 /// YOUR FUNCTION for speak

 _speak("$ITEM_IN_LIST");

 }).then(print)).then(print).catchError(print);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.