Home > Blockchain >  Flutter text to speech (flutter_tts) does not work
Flutter text to speech (flutter_tts) does not work

Time:10-30

I am trying to build this simple text to speech feature using flutter_tts package but it doesn't speak no matter what I tried (official example, tutorials, etc.)

I checked my Audio Output, it's set correctly. I have other MP3 sounds, they play just fine.

I tried ^3.5.0 and ^3.5.3 versions, neither worked.

Here is my code:

class ColorsScreen extends StatefulWidget {
  const ColorsScreen({super.key, required this.title});

  final String title;

  @override
  State<ColorsScreen> createState() => _ColorsScreenState();
}

class _ColorsScreenState extends State<ColorsScreen> {
  late FlutterTts flutterTts;
  bool isPlaying = false;
  String _currentColor = "black";
  String get currentColorName =>_currentColor;

  @override
  void initState() {
    super.initState();
    initTts();

    flutterTts.setStartHandler(() {
      setState(() {
        isPlaying = true;
      });
    });

    flutterTts.setCompletionHandler(() {
      setState(() {
        isPlaying = false;
      });
    });
  }

  initTts() async {
    flutterTts = FlutterTts();
  }

  Future _readColorName() async {
    await flutterTts.setVolume(1);
    await flutterTts.setSpeechRate(1);
    await flutterTts.setPitch(1);
    var result = await flutterTts.speak(currentColorName);
    if (result == 1) {
      setState(() {
        isPlaying = true;
      });
    }
  }

  @override
  void dispose() {
    super.dispose();
    flutterTts.stop();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TopBar(widget.title),
      body: IconButton(
        padding: const EdgeInsets.all(0),
        icon: const Icon(
          Icons.play_circle_fill_rounded,
          color: Colors.red,
          size: 50,
          semanticLabel: 'Play color name',
        ),
        onPressed: _readColorName,
      ),
    );
  }
}

CodePudding user response:

Try in different real android devices such as: Android 11 or Android 12

CodePudding user response:

I had recently upgraded my simulator to iOS 16.0 and now realizing that's the problem. Switched to a iOS 15.5 simulator and it worked fine. When on iOS 16.0, XCode console was showing this error: "unable to list voice folder" Leaving it here in case someone else faces a similar problem. I will continue building in iOS 15.5 version for now and keep checking if the libraries get any updates.

  • Related