huu huu - 6 months ago 86
Javascript Question

SpeechSynthesis API onend callback not working

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
.

Answer

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.

Demo: http://jsfiddle.net/QYw6b/2/

you get time passed and both events fired for sure.