在实现文本转语音时,代码执行在 flutter 中的异步函数中的 wait 处停止
我正在尝试颤振中的文本到语音功能,但它不起作用。 我尝试了互联网上的许多代码示例并用谷歌搜索,但没有找到为什么不能在我的项目中工作。
为了简单起见,我尝试使用一个简单的文件,但它仍然不起作用。
单击按钮后,第一个打印语句正在执行,但之后没有得到任何结果。
下面是代码
import 'package:flutter/material.dart';
import 'package:flutter_tts/flutter_tts.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late FlutterTts flutterTts;
@override
initState() {
super.initState();
initTts();
}
initTts() {
flutterTts = FlutterTts();
}
@override
void dispose() {
super.dispose();
flutterTts.stop();
}
Future _speak() async {
print("1");
await flutterTts.speak("Hello");
print("2");
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter TTS'),
),
body: InkWell(
onTap: _speak,
child: Text("Click"),
)
),
);
} Performing
控制台输出为
hot reload...
Syncing files to device Android SDKbuilt for x86...
Reloaded 1 of 568 Library in 544ms。
我/颤振(21300):1
I am trying the text to speech feature in a flutter, but it is not working.
I tried many code examples from the internet and googled but did not find why not working in my project.
To keep it simple I was trying in one simple file but still, it is not working.
On click of a button, the first print statement is executing but after that not get anything.
Below is the code
import 'package:flutter/material.dart';
import 'package:flutter_tts/flutter_tts.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late FlutterTts flutterTts;
@override
initState() {
super.initState();
initTts();
}
initTts() {
flutterTts = FlutterTts();
}
@override
void dispose() {
super.dispose();
flutterTts.stop();
}
Future _speak() async {
print("1");
await flutterTts.speak("Hello");
print("2");
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter TTS'),
),
body: InkWell(
onTap: _speak,
child: Text("Click"),
)
),
);
}
}
The console output is
Performing hot reload...
Syncing files to device Android SDK built for x86...
Reloaded 1 of 568 libraries in 544ms.
I/flutter (21300): 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论