react-native-android-text-to-speech
v1.2.3
Published
React Native Text-To-Speech wrapper module for android
Downloads
11
Maintainers
Readme
react-native-android-text-to-speech
Getting started
npm install --save react-native-android-text-to-speech
Mostly automatic installation
react-native link react-native-android-text-to-speech
Manual installation
Android
- Open up
android/app/src/main/java/[...]/MainApplication.java
- Add
import com.echo.reactandroidtts.RNAndroidTextToSpeechPackage;
to the imports at the top of the file - Add
new RNAndroidTextToSpeechPackage()
to the list returned by thegetPackages()
method
- Append the following lines to
android/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')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:compile project(':react-native-android-text-to-speech')
Usage
This wrapper library's function uses Promises instead of callbacks. The ".then()" or es2016's async await
(recommended) keywords should be used.
Imports
import AndroidTextToSpeech from 'react-native-tts';
Speaking
- Use the function
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');
- Use the function
AndroidTextToSpeech.stop()
to stop speaking and flush the TTS play queue. Return a promise with "success".
AndroidTextToSpeech.stop();
Ducking
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)
List Attribute Information
Various lists can be retrieved regarding the TTS Engine.
- Installed Engines Info: Use function
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.
- Current Engine Info: Use function
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.
- All Available Locales: Use function
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
}
- Default Locale: Use Function
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
}
- Current Locale: Use Function
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
}
- All Available Voices: Use function
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
}
- Current Voice: Use function
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
}
Set Speech Attributes
Various speech attributes can be set
- Language: Use function
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.
- Pitch: Use function
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".
- Speech Rate: Use function
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".
Events
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-cancel', (event) => console.log("cancel", event));
Example Code
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;
}
License
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
- You just DO WHAT THE FUCK YOU WANT TO.