Flutter: Text to Voice Array

4,914

Solution 1

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

Solution 2

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");
Share:
4,914
Dev Ed
Author by

Dev Ed

Updated on December 04, 2022

Comments

  • Dev Ed
    Dev Ed over 1 year

    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.

  • Dev Ed
    Dev Ed about 6 years
    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.
  • Ashton Thomas
    Ashton Thomas about 6 years
    @DevEd I've updated the answer to include a snippet of how you can get this to work in Flutter.
  • Dev Ed
    Dev Ed about 6 years
    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!
  • Dev Ed
    Dev Ed almost 6 years
    It worked!! It took me a few minutes to understand but its Finally Running and Working!! Cheers!!!