首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Soundmanager2不会从谷歌翻译加载声音

Soundmanager2不会从谷歌翻译加载声音
EN

Stack Overflow用户
提问于 2013-05-29 17:33:58
回答 2查看 456关注 0票数 1

我想说一些文本;如果我在浏览器中输入一个格式正确的url,我可以从谷歌翻译tts获得音频文件(Mp3)。

但是如果我尝试createSound它,我只在firebug中看到一个404-error。

我使用这个,但它失败了:

代码语言:javascript
复制
soundManager.createSound( 
  {id:'testsound', 
   autoLoad:true,
   url:'http://translate.google.com/translate_tts?ie=UTF-8&tl=da&q=testing'}
 );

我已经用wget预取了固定的语音提示,所以它们是同一台网页服务器上的本地mp3文件。但我想说的是动态提示。

EN

回答 2

Stack Overflow用户

发布于 2014-03-29 17:14:40

我知道这个问题很久以前就有人问过了,但我也遇到了类似的问题,我能够让它在Chrome和Firefox上运行,但使用的是Audio标签。

这是我制作的演示页面

http://jsfiddle.net/royriojas/SE6ET/

下面是为我做这件事的代码。

代码语言:javascript
复制
var sayIt;

function createSayIt() {

    // Tiny trick to make the request to google actually work!, they deny the request if it comes from a page but somehow it works when the function is inside this iframe!

    //create an iframe without setting the src attribute
    var iframe = document.createElement('iframe');

    // don't know if the attribute is required, but it was on the codepen page where this code worked, so I just put this here. Might be not needed.
    iframe.setAttribute('sandbox', 'allow-scripts allow-same-origin allow-pointer-lock');
    // hide the iframe... cause you know, it is ugly letting iframes be visible around...
    iframe.setAttribute('class', 'hidden-iframe')

    // append it to the body
    document.body.appendChild(iframe);

    // obtain a reference to the contentWindow
    var v = iframe.contentWindow;

    // parse the sayIt function in this contentWindow scope
    // Yeah, I know eval is evil, but its evilness fixed this issue...
    v.eval("function sayIt(query, language, cb) { var audio = new Audio(); audio.src = 'http://translate.google.com/translate_tts?ie=utf-8&tl=' + language + '&q=' + encodeURIComponent(query); cb && audio.addEventListener('ended', cb);  audio.play();}");

    // export it under sayIt variable
    sayIt = v.sayIt;
}

我想我能够byPass那个限制。他们可能在未来修复这个黑客攻击,我不知道。我真的希望他们不要..。

您也可以尝试使用Text2Speech HTML5应用程序接口,但它仍然非常年轻……

IE 11不能解决这个问题,将来我可能会尝试修复它

票数 1
EN

Stack Overflow用户

发布于 2013-06-01 21:58:33

尽管您认为这是一个404错误,但您实际上遇到了跨域限制。

来自404的一些响应头也会给你一个线索,告诉你发生了什么:

代码语言:javascript
复制
X-Content-Type-Options:nosniff
X-XSS-Protection:1; mode=block

因此,您无法在客户端执行此操作,因为Google不允许(也可能永远不会)允许您执行此操作。

为了实现音频的动态加载,您需要绕过这个x域限制,设置一个proxy on your own server,它可以从google的服务器(通过wget或其他方式)下载最终用户请求的任何文件,并输出来自google的任何数据。

我用来重现这个问题的代码:

代码语言:javascript
复制
soundManager.setup({
    url: 'swf',
    onready: function() {
        soundManager.createSound({
            id:'testsound', 
            autoLoad:true,
            url:'http://translate.google.com/translate_tts?ie=UTF-8&tl=da&q=testing'
        });
    }
});

您的代码应如下所示:

代码语言:javascript
复制
soundManager.createSound({
    id:'testsound', 
    autoLoad:true,
    url:'/audioproxy.php?ie=UTF-8&tl=da&q=testing' // Same domain!
});

致以问候并祝你好运!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16810327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档