javascript 在服务器上以 HTML5 格式播放远程音频(来自 Google 翻译)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5885475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Playing remote audio (from Google Translate) in HTML5 on a server
提问by TomTasche
I'm trying to use text-to-speech on a website using HTML5 and Google Translate.
我正在尝试在使用 HTML5 和 Google 翻译的网站上使用文本到语音转换。
Getting speech from Google is as easy as a GET request to: http://translate.google.com/translate_tts?tl=en&q=hello
从 Google 获取语音就像向以下地址发送 GET 请求一样简单:http: //translate.google.com/translate_tts?tl=en&q=hello
In order to play that file I'm using the audio-tag:
为了播放该文件,我使用了音频标签:
<audio id="speech" src="http://translate.google.com/translate_tts?tl=en&q=hello" controls="controls" autoplay="autoplay">Your browser does not support the audio element.</audio>
<audio id="speech" src="http://translate.google.com/translate_tts?tl=en&q=hello" controls="controls" autoplay="autoplay">Your browser does not support the audio element.</audio>
That works perfectly when I try to open the html file locally using Chrome 11, but doesn't work at all when I open the html from my server... It just doesn't do anything (the play button flashes for a second, but nothing happens).
当我尝试使用 Chrome 11 在本地打开 html 文件时,这非常有效,但是当我从服务器打开 html 时根本不起作用......它什么也没做(播放按钮闪烁一秒钟,但没有任何反应)。
You can find the file here: http://www.announcify.com/chrome/background.html
您可以在此处找到该文件:http: //www.announcify.com/chrome/background.html
Any ideas? :)
Tom
有任何想法吗?:)
汤姆
采纳答案by MKN Web Solutions
Make sure your rel tags are set up correctly. There's a possibility that Google has a cross domain protection.
确保您的 rel 标签设置正确。谷歌可能有跨域保护。
回答by Piotr Sobczyk
NodeJS equivalent for accepted answer (formulated in comments) is:
NodeJS 等效于接受的答案(在评论中制定)是:
app.route("/api/tts").get(function(req,res){
res.type('audio/mpeg');
var text = req.query.q;
var request = require('request');
var url = "https://translate.google.pl/translate_tts?ie=UTF-8&q=" + text + "&tl=en&total=1&idx=0&client=t&prev=input";
request.get(url).pipe(res);
});
Client should send url-encoded text as a query param q, e.g. host/api/tts?q=Hello
客户端应该发送 url 编码的文本作为查询参数 q,例如 host/api/tts?q=Hello