views:

118

answers:

1

hello everybody! i'm new here but i hope you can help me.

i'm trying to create a button that plays the text to speech mp3 that google generates. i'm creating a translator, so, what i want is to do something like google translate is (in some way).

i've tried with javascript and actionscript but i couldn't make it work.

i have this javascript function:

function audio () {
    // here i get the word that i want to hear
    texto = document.getElementById('txt-result-palabra').innerHTML;
    // now i get the language
    idioma = document.getElementById("id-traducir-palabra").value;
    url = "http://translate.google.com/translate_tts?q=";
    url += texto;
    url += "&tl=";
    url += idioma;
    }  

so, with this function i actually have the url of the google tts for some word, but i don't know how to embed it, o which is the best way to do it. i mean, i can embed it with javascript, but i'm not sure if it's gonna work since the file that google generates is an mp3.

and i need this mp3 to be played onClick of an image...

i also wonder if it could be done with html5.

if anyone knows any solution i'd appreciate it a lot!

thanks in advance and have a great day!!!

A: 

Assuming you have the url to the MP3, you need to append to the document

<audio autoplay="autoplay">
  <source src="url_to_google_tts.mp3" type="audio/mpeg">
</audio>

...

var audioObj = document.createElement("audio");
audioObj.autoplay = "autoplay";

var sourceObj = document.createElement("source");
sourceObj.src = "url_to_google.mp3";
sourceObj.type= "audio/mpeg";
audioObj.appendChild(sourceObj);

document.body.appendChild(audioObj);
ItzWarty
hi ItzWarty! thanks for answering!!!my problem is that i construct the mp3 url dynamically with javascript. so, how could assign the result of the funcion to the src?thanks again!!!
sole
thanks a lot IltsWarty, i could do it with your information!!!! great! :D
sole
@sole - if his answer solved your problem, please accept it.
Matt Ball
it works fine, the problem is that not every browser accept mp3 with html5, and this is not good. i wonder if it's possible to do something similar with flash? thanks!
sole