SpeechSynthesis API onend回调不起作用 [英] SpeechSynthesis API onend callback not working

查看:2539
本文介绍了SpeechSynthesis API onend回调不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用演讲稿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 $虽然这是我发现它的工作原理,但我不确定这是否是正确的行为....

$ b

$ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆