UNPKG

react-native-gtts-lite

Version:

🎙️ Lightweight Google Translate TTS for React Native CLI using react-native-fs and react-native-sound.

36 lines (30 loc) 983 B
const Sound = require('react-native-sound'); const BASE_URL = 'https://translate.google.com/translate_tts'; /** * Speak the given text using Google Translate TTS (streamed). * Requires Internet connection. This version uses only react-native-sound. */ async function speak(text, lang = 'en') { try { const url = `${BASE_URL}?ie=UTF-8&q=${encodeURIComponent(text)}&tl=${lang}&client=tw-ob`; // NOTE: react-native-sound does not support direct streaming from URL. // This version attempts playback but may not work on all devices without local download. const sound = new Sound(url, null, (error) => { if (error) { console.error('Sound load error:', error); return; } sound.play((success) => { if (!success) { console.error('Playback failed'); } sound.release(); // clean up }); }); } catch (err) { console.error('speak() failed:', err); } } module.exports = { speak, };