开发者

how to create a button that plays a mp3 google tts

开发者 https://www.devze.com 2023-01-08 19:48 出处:网络
i\'m new here but i hope you can help me. i\'m try开发者_C百科ing 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 some

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

i'm try开发者_C百科ing 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!!!


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消