【发布时间】:2021-02-10 04:31:13
【问题描述】:
我有一个程序可以将文本翻译成语音,在颤振中,到目前为止一切都很好,问题是如何让程序显示一个文本,说明程序中“正在播放”和“未播放”的状态,即在播放语音的同时,显示“正在播放”状态。
非常感谢您的回复。
这是我的代码:
import 'package:flutter/material.dart';
import 'package:flutter_tts/flutter_tts.dart';
class TextVoicePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: TextVoice(),
);
}
}
class TextVoice extends StatefulWidget {
@override
_Speak createState() => _Speak();
}
class _Speak extends State<TextVoice> {
final FlutterTts flutterTts = FlutterTts();
@override
Widget build(BuildContext context) {
bool speaking = false;
TextEditingController textEditingController = TextEditingController();
Future _speak(String text) async {
await flutterTts.setLanguage("es-MX");
await flutterTts.setPitch(1);
await flutterTts.speak(text);
}
Future _stop() async {
await flutterTts.stop();
}
return Scaffold(
body: Stack(
children: <Widget>[
Padding(
padding: EdgeInsets.all(30.0),
child: TextFormField(
controller: textEditingController,
),
),
],
),
floatingActionButtonLocation:
FloatingActionButtonLocation.miniCenterDocked,
floatingActionButton: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FloatingActionButton(
backgroundColor: Colors.green,
foregroundColor: Colors.white,
heroTag: "btn",
onPressed: () => _speak(textEditingController.text),
child: Icon(Icons.play_arrow),
),
SizedBox(
width: 130,
),
FloatingActionButton(
foregroundColor: Colors.white,
backgroundColor: Colors.red,
heroTag: "btn2",
onPressed: () => _stop(),
child: Icon(Icons.stop),
)
],
),
));
}
}
【问题讨论】:
标签: flutter dart text-to-speech status