Comenzando
-
Instala el paquete
Ventana de terminal npm i @capgo/capacitor-speech-synthesisVentana de terminal pnpm add @capgo/capacitor-speech-synthesisVentana de terminal yarn add @capgo/capacitor-speech-synthesisVentana de terminal bun add @capgo/capacitor-speech-synthesis -
Sincroniza con proyectos nativos
Ventana de terminal npx cap syncVentana de terminal pnpm cap syncVentana de terminal yarn cap syncVentana de terminal bunx cap sync
Importa el plugin y sintetiza voz desde texto:
import { SpeechSynthesis } from '@capgo/capacitor-speech-synthesis';
// Texto a voz básicoconst speak = async () => { await SpeechSynthesis.speak({ text: '¡Hola, mundo!', language: 'es-ES' });};
// Uso avanzado con control de vozconst speakWithVoice = async () => { await SpeechSynthesis.speak({ text: 'Esta es una prueba de síntesis de voz.', language: 'es-ES', rate: 1.0, // Velocidad de habla: 0.1 a 10.0 pitch: 1.0, // Tono de voz: 0.5 a 2.0 volume: 1.0, // Volumen: 0.0 a 1.0 voice: 'com.apple.ttsbundle.Samantha-compact' });};Referencia de API
Section titled “Referencia de API”speak(options)
Section titled “speak(options)”Habla el texto dado usando la configuración de voz especificada.
interface SpeakOptions { text: string; // Texto a hablar language?: string; // Código de idioma (ej., 'es-ES', 'en-US') rate?: number; // Velocidad de habla: 0.1 a 10.0 (predeterminado: 1.0) pitch?: number; // Tono de voz: 0.5 a 2.0 (predeterminado: 1.0) volume?: number; // Volumen: 0.0 a 1.0 (predeterminado: 1.0) voice?: string; // Identificador de voz (obtener de getVoices())}
await SpeechSynthesis.speak(options);stop()
Section titled “stop()”Detiene cualquier síntesis de voz en curso.
await SpeechSynthesis.stop();getVoices()
Section titled “getVoices()”Obtiene la lista de voces disponibles para síntesis de voz.
interface Voice { voiceURI: string; // Identificador único de voz name: string; // Nombre de voz legible por humanos lang: string; // Código de idioma default: boolean; // Si esta es la voz predeterminada}
const { voices } = await SpeechSynthesis.getVoices();console.log('Voces disponibles:', voices);getSupportedLanguages()
Section titled “getSupportedLanguages()”Obtiene la lista de códigos de idioma soportados.
const { languages } = await SpeechSynthesis.getSupportedLanguages();console.log('Idiomas soportados:', languages);isSpeaking()
Section titled “isSpeaking()”Verifica si la síntesis de voz está actualmente activa.
const { speaking } = await SpeechSynthesis.isSpeaking();console.log('Hablando actualmente:', speaking);Ejemplo Completo
Section titled “Ejemplo Completo”import { SpeechSynthesis } from '@capgo/capacitor-speech-synthesis';
export class TextToSpeechService { private currentVoice: string | undefined;
async init() { // Obtener voces disponibles const { voices } = await SpeechSynthesis.getVoices();
// Encontrar voz en español const spanishVoice = voices.find(v => v.lang.startsWith('es')); this.currentVoice = spanishVoice?.voiceURI;
console.log(`Usando voz: ${spanishVoice?.name}`); }
async speak(text: string, options?: Partial<SpeakOptions>) { try { // Detener cualquier habla en curso await SpeechSynthesis.stop();
// Hablar el texto await SpeechSynthesis.speak({ text, language: 'es-ES', rate: 1.0, pitch: 1.0, volume: 1.0, voice: this.currentVoice, ...options }); } catch (error) { console.error('Síntesis de voz falló:', error); throw error; } }
async speakWithRate(text: string, rate: number) { await this.speak(text, { rate }); }
async stop() { await SpeechSynthesis.stop(); }
async checkIfSpeaking(): Promise<boolean> { const { speaking } = await SpeechSynthesis.isSpeaking(); return speaking; }
async listVoicesByLanguage(languageCode: string) { const { voices } = await SpeechSynthesis.getVoices(); return voices.filter(v => v.lang.startsWith(languageCode)); }}Uso Avanzado
Section titled “Uso Avanzado”Soporte Multi-idioma
Section titled “Soporte Multi-idioma”// Hablar en diferentes idiomasconst speakMultiLanguage = async () => { await SpeechSynthesis.speak({ text: '¡Hola!', language: 'es-ES' });
await SpeechSynthesis.speak({ text: 'Bonjour!', language: 'fr-FR' });
await SpeechSynthesis.speak({ text: 'こんにちは!', language: 'ja-JP' });};Selección de Voz
Section titled “Selección de Voz”// Seleccionar una voz específicaconst selectVoice = async (languageCode: string) => { const { voices } = await SpeechSynthesis.getVoices();
// Filtrar voces por idioma const languageVoices = voices.filter(v => v.lang.startsWith(languageCode) );
// Usar la primera voz disponible if (languageVoices.length > 0) { await SpeechSynthesis.speak({ text: 'Probando selección de voz', language: languageCode, voice: languageVoices[0].voiceURI }); }};Lectura de Texto Largo
Section titled “Lectura de Texto Largo”// Dividir y hablar texto largo en fragmentosconst speakLongText = async (longText: string) => { const sentences = longText.match(/[^.!?]+[.!?]+/g) || [longText];
for (const sentence of sentences) { await SpeechSynthesis.speak({ text: sentence.trim(), language: 'es-ES', rate: 0.9 });
// Esperar un poco entre oraciones await new Promise(resolve => setTimeout(resolve, 500)); }};Mejores Prácticas
Section titled “Mejores Prácticas”- Selección de Voz: Siempre verifica las voces disponibles antes de usar un ID de voz específico
- Códigos de Idioma: Usa códigos de idioma estándar (ej., ‘es-ES’, ‘en-US’, ‘fr-FR’)
- Control de Velocidad: Mantén la velocidad entre 0.5 y 2.0 para un habla de sonido natural
- Detener Antes de Hablar: Llama a stop() antes de iniciar un nuevo habla para evitar superposición
- Manejo de Errores: Envuelve todas las llamadas en bloques try-catch para un manejo de errores robusto
Solución de Problemas
Section titled “Solución de Problemas”Problemas Comunes
Section titled “Problemas Comunes”Sin salida de voz: Verifica el volumen del dispositivo y asegúrate de que el texto no esté vacío Voz no encontrada: Usa getVoices() para encontrar voces disponibles en el dispositivo Habla interrumpida: Detén cualquier habla en curso antes de iniciar una nueva síntesis Idioma no soportado: Verifica getSupportedLanguages() para idiomas disponibles