Home > Uncategorized > Text to Speech in Javascript

Text to Speech in Javascript

Just a short post about an undocumented Google API, Text-To-Speech.

function sayWord(word,lang)
{
var url = “http://translate.google.com/translate_tts?ie=UTF-8&q=” + word.toLowerCase() + “&tl=” + lang;
console.log(url);
var snd = new Audio(url);
snd.play();
}

Where “Word” is the phrase to say, and lang is the accent of the speaker. For example “Chat” in french is pronounced “Shaa’.

An important “Gotya” is that in iOS, this function must only be called in response to a click event. You cannot autoplay audio.

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment