如何创建一个播放MP3谷歌的按钮 [英] how to create a button that plays a mp3 google tts
问题描述
我是新来的,但我希望你可以帮助我。
i'm new here but i hope you can help me.
我正在尝试创建一个播放谷歌生成的文本到语音mp3的按钮。我正在创建一个翻译器,所以,我想要的是做一些像谷歌翻译(在某种程度上)。
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).
我尝试过使用javascript和actionscript但是我无法使它工作。
i've tried with javascript and actionscript but i couldn't make it work.
我有这个javascript函数:
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;
}
因此,使用此功能我实际上有一些谷歌的网址单词,但我不知道如何嵌入它,o这是最好的方法。我的意思是,我可以用javascript嵌入它,但我不确定它是否会起作用,因为谷歌生成的文件是一个mp3。
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.
我需要这个mp3才能播放点击图片...
and i need this mp3 to be played onClick of an image...
我也想知道它是否可以可以用html5完成。
i also wonder if it could be done with html5.
如果有人知道任何解决方案,我会非常感激!
if anyone knows any solution i'd appreciate it a lot!
谢谢提前祝你好运!
推荐答案
假设你有MP3的网址,你需要附加到文件
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);
这篇关于如何创建一个播放MP3谷歌的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!