[英]Google translate Text-to-speech action canceled
I have write a function who gets the audio element from Google Translate: 我编写了一个从Google Translate获取音频元素的函数:
Interface.prototype.say = function( text ) {
var audio = new Audio();
audio.src ='http://translate.google.com/translate_tts?ie=utf-8&tl=fr&q=' + escape( text );
audio.play();
console.log( audio.src );
}
audio.src
returns the right string to translate but It didn't say anything. audio.src
返回正确的字符串进行翻译,但未说明任何内容。 In console, in Network section I found that: 在控制台的“网络”部分中,我发现:
translate_tts?ie=utf-8&tl=fr&q=Initialisation%20termine
translate.google.com
GET
(canceled)
Pending
game.php:1
Parser
As you can see, the file request was canceled. 如您所见,文件请求已被取消。 If I click on the link (first line), it works very well.
如果我单击链接(第一行),则效果很好。 It seems that it's not working only when I try to request the file to another domain, but it works perfectly in the Google Translate domain.
看来,仅当我尝试将文件请求到另一个域时,它才不起作用,但是在Google Translate域中它却可以正常工作。
What is the problem? 问题是什么?
Solution: 解:
Interface.prototype.say = function( text ) {
var section, frame;
section = document.getElementsByTagName( "head" )[ 0 ];
frame = document.createElement( "iframe" );
frame.src = 'http://translate.google.com/translate_tts?ie=utf-8&tl=fr&q=' + escape( text );
section.appendChild( frame );
}
Set an Iframe to the SRC. 将Iframe设置为SRC。 It will play automatically, or set an onload handler for the audio that then calls
play
. 它将自动播放,或为音频设置onload处理程序,然后调用
play
。 It's likely because you're forcing it to play before it has downloaded. 可能是因为您强迫它在下载之前播放。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.