Skip to content

yuefeng007/flutter_tts

 
 

Repository files navigation

Text To Speech

pub package

A flutter text to speech plugin (Swift,Java)

Features

  • Android, iOS, & Web
    • speak
    • stop
    • get languages
    • set language
    • set speech rate
    • set speech volume
    • set speech pitch
    • is language available
  • Android, iOS
    • get voices
    • set voice
    • speech marks (requires iOS 7+ and Android 26+)
  • Android
    • set Silence
    • synthesize to file
  • iOS
    • set shared instance
    • pause
    • set audio session category

Usage

Web

Website from the example directory.

Android

Change the minimum Android sdk version to 21 (or higher) in your android/app/build.gradle file.

minSdkVersion 21

iOS

There's a known issue with integrating plugins that use Swift into a Flutter project created with the Objective-C template. Flutter#16049

Example

To use this plugin :

  dependencies:
    flutter:
      sdk: flutter
    flutter_tts:
  • instantiate FlutterTts
FlutterTts flutterTts = FlutterTts();

To set shared audio instance (iOS only):

await flutterTts.setSharedInstance(true);

To set audio category and options (iOS only):

await flutterTts
        .setIosAudioCategory(IosTextToSpeechAudioCategory.playAndRecord, [
      IosTextToSpeechAudioCategoryOptions.allowBluetooth,
      IosTextToSpeechAudioCategoryOptions.allowBluetoothA2DP,
      IosTextToSpeechAudioCategoryOptions.mixWithOthers
    ]);

speak, stop, getLanguages, setLanguage, setSpeechRate, setVolume, setPitch, isLanguageAvailable, setSharedInstance

Future _speak() async{
    var result = await flutterTts.speak("Hello World");
    if (result == 1) setState(() => ttsState = TtsState.playing);
}

Future _stop() async{
    var result = await flutterTts.stop();
    if (result == 1) setState(() => ttsState = TtsState.stopped);
}

List<dynamic> languages = await flutterTts.getLanguages;

await flutterTts.setLanguage("en-US");

await flutterTts.setSpeechRate(1.0);

await flutterTts.setVolume(1.0);

await flutterTts.setPitch(1.0);

await flutterTts.isLanguageAvailable("en-US");

// iOS only
await flutterTts.setSharedInstance(true);

await flutterTts.pause();

// Android only
await flutterTts.synthesizeToFile("Hello World", "tts.wav");

await flutterTts.setSilence(2);

Listening for platform calls

flutterTts.setStartHandler(() {
  setState(() {
    ttsState = TtsState.playing;
  });
});

flutterTts.setCompletionHandler(() {
  setState(() {
    ttsState = TtsState.stopped;
  });
});

flutterTts.setProgressHandler((String text, int startOffset, int endOffset, String word) {
  setState(() {
    _currentWord = word;
  });
});

flutterTts.setErrorHandler((msg) {
  setState(() {
    ttsState = TtsState.stopped;
  });
});

flutterTts.setCancelHandler((msg) {
  setState(() {
    ttsState = TtsState.stopped;
  });
});

// iOS only
flutterTts.setPauseHandler((msg) {
  setState(() {
    ttsState = TtsState.paused;
  });
});

flutterTts.setContinueHandler((msg) {
  setState(() {
    ttsState = TtsState.continued;
  });
});

Getting Started

For help getting started with Flutter, view our online documentation.

For help on editing plugin code, view the documentation.

About

Flutter Text to Speech package

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Dart 41.6%
  • Java 24.9%
  • Swift 22.3%
  • Ruby 9.4%
  • Objective-C 0.7%
  • Kotlin 0.7%
  • HTML 0.4%