InfiniDaZa
InfiniDaZa

Reputation: 67

Trouble with creating and playing audio element

Ok, I am trying to use the google translate speech api to say something. Here's my code:

<script>
function say(words){
var a=new Audio();
a.src='http://translate.google.com/translate_tts?q='+words;
a.play();
};
</script>

<div id="button onclick="say('hello');">Click to say hello!</div>

When I click the button, nothing happens. Can anyone see where I've gone wrong? Thanks.

Upvotes: 0

Views: 88

Answers (1)

Rob W
Rob W

Reputation: 349122

You need to add the language (tl=) parameter (and add a closing quote after your ID attribute). Here's an example using English:

function say(words) {
    var a = new Audio();
    a.src = 'http://translate.google.com/translate_tts?tl=en&q=' + words;
    a.play();
}

Google's TTS API does not return any content if a non-matching Referrer header is set though.

Upvotes: 1

Related Questions