Sunday, May 19, 2024
167
rated 0 times [  172] [ 5]  / answers: 1 / hits: 17977  / 10 Years ago, tue, may 6, 2014, 12:00:00

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.


More From » google-chrome

 Answers
20

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.


[#71167] Sunday, May 4, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
andrea

Total Points: 445
Total Questions: 95
Total Answers: 103

Location: Tonga
Member since Tue, Nov 30, 2021
3 Years ago
;