在实现文本转语音时,代码执行在 flutter 中的异步函数中的 wait 处停止

发布于 2025-01-11 14:07:31 字数 1244 浏览 0 评论 0原文

我正在尝试颤振中的文本到语音功能,但它不起作用。 我尝试了互联网上的许多代码示例并用谷歌搜索,但没有找到为什么不能在我的项目中工作。

为了简单起见,我尝试使用一个简单的文件,但它仍然不起作用。

单击按钮后,第一个打印语句正在执行,但之后没有得到任何结果。

下面是代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文