npm install --save react-native-android-text-to-speechreact-native link react-native-android-text-to-speechandroid/app/src/main/java/[...]/MainApplication.javaimport com.echo.reactandroidtts.RNAndroidTextToSpeechPackage; to the imports at the top of the filenew RNAndroidTextToSpeechPackage() to the list returned by the getPackages() methodandroid/settings.gradle:
include ':react-native-android-text-to-speech'
project(':react-native-android-text-to-speech').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-android-text-to-speech/android')
android/app/build.gradle:
compile project(':react-native-android-text-to-speech')
This wrapper library's function uses Promises instead of callbacks. The ".then()" or es2016's async await(recommended) keywords should be used.
import AndroidTextToSpeech from 'react-native-tts';AndroidTextToSpeech.speak(utterance, queueMode) to use the default TTS engine to speak. Where
'utterance' is of type string.
'queueMode' can have values -
1. "ADD" - to add to the TTS play queue.
2. "FLUSH" - to interrupt the TTS play queue with the utterance and then flush it.
Returns a promise with utteranceId.Example -
AndroidTextToSpeech.speak('Hello, world!', 'ADD');AndroidTextToSpeech.stop() to stop speaking and flush the TTS play queue.
Return a promise with "success".AndroidTextToSpeech.stop();Ducking is meant to lower other applications output sound level while speaking.
It can be enabled by using function AndroidTextToSpeech.setDucking(enable). Where 'enable' is a boolean value.
Return a promise with success.
Example-
AndroidTextToSpeech.setDucking(true)Various lists can be retrieved regarding the TTS Engine.
AndroidTextToSpeech.getEnginesInfo();to list all the available TTS Engines on the android device. Returns a promise with list containing strings of package names of the installed engines.
AndroidTextToSpeech.getCurrentEngineInfo();to get the name of current TTS Engine being used. Returns a promise with string of the package name of currently in use TTS engine.
AndroidTextToSpeech.getAvailableLocales();to get details of all the available languages of the engine. Returns a promise with an object of the form -
{
languageName: string; //Language display name as given by android
languageCode: string; //Language code according to ISO 639-2 standards
coutryName: string; //Country display name as given by android
countryCode: string; //Country code according to ISO 3166-1 alpha-3 standards
}AndroidTextToSpeech.getDefaultLocale();to get details of the default locale being used by the engine. Returns a promise with an object of the form -
{
languageName: string; //Language display name as given by android
languageCode: string; //Language code according to ISO 639-2 standards
coutryName: string; //Country display name as given by android
countryCode: string; //Country code according to ISO 3166-1 alpha-3 standards
}AndroidTextToSpeech.getCurrentLocale();to get details of the current locale being used by the engine. Returns a promise with an object of the form -
{
languageName: string; //Language display name as given by android
languageCode: string; //Language code according to ISO 639-2 standards
coutryName: string; //Country display name as given by android
countryCode: string; //Country code according to ISO 3166-1 alpha-3 standards
}AndroidTextToSpeech.getAvailableVoices();to get details of all the available voices in the engine. Returns a promise with an object of the form -
{
voiceName: string; //Name of the voice.
languageName: string; //Language display name as given by android
languageCode: string; //Language code according to ISO 639-2 standards
coutryName: string; //Country display name as given by android
countryCode: string; //Country code according to ISO 3166-1 alpha-3 standards
}AndroidTextToSpeech.getAvailableVoices();to get details of the current voice being used by the engine. Returns a promise with an object of the form -
{
voiceName: string; //Name of the voice.
languageName: string; //Language display name as given by android
languageCode: string; //Language code according to ISO 639-2 standards
coutryName: string; //Country display name as given by android
countryCode: string; //Country code according to ISO 3166-1 alpha-3 standards
}Various speech attributes can be set
AndroidTextToSpeech.setDefaultLangauge(languageCode);to set the language/locale to be used by the engine. Where 'languageCode' is in the format of ISO 639-2 standards. Returns a promise with either "success" or with error depending on the language being found.
AndroidTextToSpeech.setDefaultPitch(pitch);to set the voice pitch to be used by the engine. Where 'pitch' is speech pitch. 1.0 is the normal pitch, lower values lower the tone of the synthesized voice, greater values increase it. Returns a promise with "success".
AndroidTextToSpeech.setDefaultSpeechRate(speechRate);to set the playback speed to be used by the engine. Where 'speechRate' is speech rate. 1.0 is the normal speech rate, lower values slow down the speech (0.5 is half the normal speech rate), greater values accelerate it (2.0 is twice the normal speech rate). Returns a promise with "success".
Subscibe to TTS events.
AndroidTextToSpeech.addEventListener('tts-start', (event) => console.log("start", event));
AndroidTextToSpeech.addEventListener('tts-finish', (event) => console.log("finish", event));
AndroidTextToSpeech.addEventListener('tts-error', (event) => console.log("error", event));async function sayHello() {
let result = await AndroidTextToSpeech.speak("Hello World!", "ADD");
console.log(result);
return result;
}
async function getLanguageDetails() {
let list = await AndroidTextToSpeech.getAvailableLocales();
console.log(list);
return list;
} DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar [email protected]
Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION