我有一个将文本转换为语音的Android应用程序。数组上的每个单词/字符串都是一个按钮,当选择时它会转换为语音。我希望在 Flutter 中实现这一点。
private TextToSpeech tts;
网格视图网格;
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);
任何人都可以在Dart/Flutter中提供解决方案吗?
谢谢。
你不能使用 tts 包,因为它与 Dart 2.0 不兼容:
选择"flutter_tts",因为它正在与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");
你可能会发现 Flutter 的tts
包很有用:
https://pub.dartlang.org/packages/tts
这是一个简单的例子
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');
}
虽然您可能会在此处找到更深入的示例:
https://pub.dartlang.org/packages/tts#-example-tab-
至于将它们连接在一起:
任何人都可以在Dart/Flutter中提供解决方案吗?
下面是一个简单的示例,使用列表来呈现列表中每个字符串的按钮以及speak
单词的onPressed
操作:
@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();
}
''' 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);
}
"如果列表很大,您可以调整持续时间">
我使用的功能:
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);