SpeechSynthesis API onend回调不起作用 [英] SpeechSynthesis API onend callback not working
问题描述
我使用演讲稿Google Chrome v34.0.1847.131上的合成API 。这个API在v33开始的Chrome中实现。
大部分的文本到语音都可以工作,除了将回调分配给 onend
。例如,以下代码:
var message = window.SpeechSynthesisUtterance(Hello world!);
message.onend = function(event){
console.log('Finished in'+ event.elapsedTime +'seconds。');
};
window.speechSynthesis.speak(message);
有时会调用 onend
,有时候不会调用它。时机似乎完全关闭。当它被调用时,打印的 elapsedTime
总是一些时代的时间,比如 1399237888
。
$ b
首先不要立即调用speak函数,使用回调函数。
2,获取时间使用 timeStamp
而不是 elapsedTime
。你也可以使用 performance.now()
。
var btn = document.getElementById('btn');
speechSynthesis.cancel()
var u = new SpeechSynthesisUtterance();
u.text =这段文字已经从最初的演示中改变了。;
var t;
u.onstart =函数(事件){
t = event.timeStamp;
console.log(t);
};
u.onend = function(event){
t = event.timeStamp - t;
console.log(event.timeStamp);
console.log((t / 1000)+seconds);
};
btn.onclick = function(){speechSynthesis.speak(u);};
演示: http://jsfiddle.net/QYw6b/2/
你会有时间过去,并且这两件事情都是肯定的。
I'm using the Speech Synthesis API on Google Chrome v34.0.1847.131. The API is implemented in Chrome starting in v33.
The text-to-speech works for the most part, except when assigning a callback to onend
. For instance, the following code:
var message = window.SpeechSynthesisUtterance("Hello world!");
message.onend = function(event) {
console.log('Finished in ' + event.elapsedTime + ' seconds.');
};
window.speechSynthesis.speak(message);
will sometimes call onend
and sometimes not call it. The timing appears to be completely off. When it does get called, the printed elapsedTime
is always some epoch time like 1399237888
.
While this is how I found it to make it work, I am not sure if this is the right behavior....
First don't call the speak function it right away, use callback.
2nd, to get time use timeStamp
instead of elapsedTime
. You could have just used performance.now()
as well.
var btn = document.getElementById('btn');
speechSynthesis.cancel()
var u = new SpeechSynthesisUtterance();
u.text = "This text was changed from the original demo.";
var t;
u.onstart = function (event) {
t = event.timeStamp;
console.log(t);
};
u.onend = function (event) {
t = event.timeStamp - t;
console.log(event.timeStamp);
console.log((t / 1000) + " seconds");
};
btn.onclick = function () {speechSynthesis.speak(u);};
Demo: http://jsfiddle.net/QYw6b/2/
you get time passed and both events fired for sure.
这篇关于SpeechSynthesis API onend回调不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!