how to create a button that plays a mp3 google tts

自作多情 提交于 2019-12-22 08:22:25

问题


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!!!


回答1:


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


来源:https://stackoverflow.com/questions/3341575/how-to-create-a-button-that-plays-a-mp3-google-tts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!