Text to Speech SDK

Make your app speak using native text-to-speech.

Written By despia

Last updated About 1 year ago

SDK Usage

// Basic usage
const message = "Hello"
const gender = "Male"

if ('speechSynthesis' in window) {
    const utterance = new SpeechSynthesisUtterance(message)
    const voices = speechSynthesis.getVoices()
    utterance.voice = voices.find(voice => voice.name.includes(gender)) || voices[0]
    speechSynthesis.speak(utterance)
}

Examples

Simple Message

const message = "Welcome to the app!"
const gender = "Female"

if ('speechSynthesis' in window) {
    const utterance = new SpeechSynthesisUtterance(message)
    const voices = speechSynthesis.getVoices()
    utterance.voice = voices.find(voice => voice.name.includes(gender)) || voices[0]
    speechSynthesis.speak(utterance)
}

With Fallback

const message = "Hello there"

if ('speechSynthesis' in window) {
    const utterance = new SpeechSynthesisUtterance(message)
    speechSynthesis.speak(utterance)
} else {
    // Show text instead
    showMessage(message)
}

Choose Voice

const message = "Hello"
const gender = "Male" // Try "Male" or "Female"

if ('speechSynthesis' in window) {
    const utterance = new SpeechSynthesisUtterance(message)
    const voices = speechSynthesis.getVoices()
    utterance.voice = voices.find(voice => voice.name.includes(gender)) || voices[0]
    speechSynthesis.speak(utterance)
}

Tips

  • Always check if speech synthesis is available

  • Provide text fallback for accessibility

  • Voice options vary by device

  • Test on multiple devices


Need help? Email us at support@despia.com