Reputation: 901
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.
Upvotes: 4
Views: 6002
Reputation: 151
''' 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);
Upvotes: 0
Reputation: 2802
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");
Upvotes: 4
Reputation: 17799
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();
}
Upvotes: 3