How to Make Text-to-Speech in Flutter

In this example, we will show you how to make or convert text to speech and play the speech sound in Flutter without any internet. You can also configure the different language, volume, pitch, and speech speeds on the text-to-speech tool in Flutter. See the example below:

First, add flutter_tts to your project by adding the following lines in pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  flutter_tts: ^3.5.1

Now, add TextToSpeech.Engine.INTENT_ACTION_TTS_SERVICE in the queries elements at AndroidManifest.xml file:

<queries>
    <intent>
        <action android:name="android.intent.action.TTS_SERVICE" />
    </intent>
</queries>

<application
        android:label="test"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">

Now, you can play text to speech with the code below:

import 'package:flutter_tts/flutter_tts.dart';
FlutterTts ftts = FlutterTts();
//play text to speech
var result = await ftts.speak("Hello World, this is Flutter Campus.");
if(result == 1){
    //speaking
}else{
  //not speaking
}

Change the text you want to play, or you can add the text field where users can input the text they want to convert to text-to-speech.

You can also configure like language, speech volume, speech speed, and pitch of the tool:

//your custom configuration
await ftts.setLanguage("en-US");
await ftts.setSpeechRate(0.5); //speed of speech
await ftts.setVolume(1.0); //volume of speech
await ftts.setPitch(1); //pitc of sound

import 'package:flutter/material.dart';
import 'package:flutter_tts/flutter_tts.dart';

void main(){
  runApp(MyApp());
}

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Home(),
    );
  }
}

class Home extends StatefulWidget{
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> { 
  FlutterTts ftts = FlutterTts();

  @override
  Widget build(BuildContext context) { 
    return  Scaffold(
          appBar: AppBar(
            title: Text("Text to Speech in Flutter"),
            backgroundColor: Colors.redAccent,
          ),
          body: Container( 
            padding: EdgeInsets.all(20),
            alignment: Alignment.center,
            child: Column(
                children: [
                    ElevatedButton(
                      onPressed:() async {

                          //your custom configuration
                          await ftts.setLanguage("en-US");
                          await ftts.setSpeechRate(0.5); //speed of speech
                          await ftts.setVolume(1.0); //volume of speech
                          await ftts.setPitch(1); //pitc of sound

                          //play text to sp
                          var result = await ftts.speak("Hello World, this is Flutter Campus.");
                          if(result == 1){
                              //speaking
                          }else{
                              //not speaking
                          }
                      }, 
                      child: Text("Text to Speech"))
                ],
            ),
          )
       );
  }
}

Copy the code to your app, and try running the code to check text-to-speech in Flutter.

In this way, you can make text-to-speech in Flutter. 

No any Comments on this Article


Please Wait...