我有一个将文字转换为语音的Android应用。 数组上的每个单词/字符串都是一个按钮,选中它后会转换为语音。 我希望在Flutter中实现这一点。
private TextToSpeech tts;
GridView网格;
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中提供解决方案吗?
谢谢。
答案 0 :(得分:2)
您可能会发现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中提供解决方案吗?
这是一个简单的示例,使用列表为列表中的每个字符串呈现按钮,并将onPressed
操作呈现为speak
单词:
@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();
}
答案 1 :(得分:0)
您不能使用tts包,因为它与Dart 2.0不兼容:
在与Dart 2.0一起使用时,请选择“ flutter_tts”
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");
答案 2 :(得分:0)
''''import'package:flutter_tts / flutter_tts.dart';'''
for (int i = 0; i < matches.Count; i++)
{
Console.WriteLine(matches[i].Value);
}
''''如果列表很大,您可以调整持续时间'''
我使用的功能:
Future _speak(String text) async{
var result = await flutterTts.speak(text);
if (result == 1) setState(() => ttsState = TtsState.playing);
}