Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 919
huu Avatar asked May 06 '14 00:05

huu


1 Answers

According to this comment on the bug mentioned in the answer from Kevin Hakanson, it might be a problem with garbage collection. Storing the utterance in a variable before calling speak seems to do the trick:

window.utterances = []; var utterance = new SpeechSynthesisUtterance( 'hello' ); utterances.push( utterance ); speechSynthesis.speak( utterance ); 
like image 70
techpeace Avatar answered Sep 29 '22 04:09

techpeace