@nammayatri/react-native-tts
v0.1.0
Published
Text-to-speech for React Native on iOS and Android. Maintained fork in the spirit of ak1394/react-native-tts.
Readme
@nammayatri/react-native-tts
Text-to-speech for React Native, iOS and Android. A maintained, TypeScript-first
re-implementation inspired by react-native-tts
(MIT, no longer actively maintained).
- Android: Kotlin module backed by
android.speech.tts.TextToSpeech - iOS: Swift module backed by
AVSpeechSynthesizer - Works on both the legacy bridge and the New Architecture (TurboModule interop)
- Strict TypeScript types for every method and event
Installation
yarn add @nammayatri/react-native-tts
# or
npm install @nammayatri/react-native-ttsThen on iOS:
cd ios && pod installNo autolinking changes are required on RN 0.60+.
iOS Swift setup note
This package ships Swift code. If your app does not already use Swift, add an
empty Swift file (e.g. Dummy.swift) to your Xcode project so Xcode generates
the Swift runtime header for you. You should not need a bridging header.
If you see build errors about React not being found, ensure your Podfile
uses modular headers or add:
use_modular_headers!or, more narrowly:
pod 'React-Core', :modular_headers => trueQuick start
import Tts from '@nammayatri/react-native-tts';
await Tts.getInitStatus();
await Tts.setDefaultLanguage('en-IN');
await Tts.setDefaultRate(0.5); // 0..1, remapped per-platform
await Tts.setDefaultPitch(1.0);
const utteranceId = await Tts.speak('Your cab is arriving.');API
All methods return a Promise. Values that aren't supported on a given
platform resolve to a neutral value (false, 'not-supported', or [])
rather than throwing, so cross-platform code can call them unconditionally.
| Method | iOS | Android | Notes |
| --- | :-: | :-: | --- |
| getInitStatus() | ✅ | ✅ | Resolves once the engine is ready. |
| speak(utterance, options?) | ✅ | ✅ | Resolves with an utteranceId string. |
| stop(onWordBoundary?) | ✅ | ✅ | |
| pause(onWordBoundary?) | ✅ | ❌ | Resolves false on Android. |
| resume() | ✅ | ❌ | Resolves false on Android. |
| setDefaultLanguage(lang) | ✅ | ✅ | BCP-47 tag (e.g. 'en-IN'). |
| setDefaultVoice(voiceId) | ✅ | ✅ | Use an id from voices(). |
| setDefaultRate(rate, skipTransform?) | ✅ | ✅ | rate in 0..1; iOS remaps to AVSpeech range unless skipTransform. |
| setDefaultPitch(pitch) | ✅ | ✅ | Clamped to 0.5..2.0 on iOS. |
| setDucking(enabled) | ✅ | ❌ | |
| setIgnoreSilentSwitch(mode) | ✅ | ❌ | 'inherit' \| 'ignore' \| 'obey'. |
| voices() | ✅ | ✅ | |
| engines() | ❌ | ✅ | |
| setDefaultEngine(name) | ❌ | ✅ | Call getInitStatus() afterwards. |
| requestInstallEngine() | ❌ | ✅ | Opens Play Store. |
| requestInstallData() | ❌ | ✅ | |
Events
const sub = Tts.addEventListener('tts-progress', (e) => {
// e: { utteranceId, start, end, length }
});
sub.remove();| Event | Payload |
| --- | --- |
| tts-start | { utteranceId } |
| tts-finish | { utteranceId } |
| tts-cancel | { utteranceId } |
| tts-progress | { utteranceId, start, end, length } |
| tts-error | { utteranceId, error? } |
| tts-pause (iOS) | { utteranceId } |
| tts-resume (iOS) | { utteranceId } |
SpeakOptions
Tts.speak('Hello', {
iosVoiceId: 'com.apple.voice.compact.en-GB.Daniel',
rate: 0.55,
androidParams: {
KEY_PARAM_PAN: 0,
KEY_PARAM_VOLUME: 1,
KEY_PARAM_STREAM: 'STREAM_MUSIC',
},
});License
MIT. See LICENSE. Based in spirit on react-native-tts by
Anton Krasovsky; this is an independent re-implementation and not an official
fork.
