Search code examples
javascriptgoogle-chromegoogle-chrome-extensiontext-to-speech

Callback in chrome.tts.speak?


I am trying to call a callback function after chrome.tts.speak has finished the speech, and there is a nice argument space for that. But, after looking for more detail about it, very disappointingly, this is what the document says:

callback ( optional function )
  Called right away, before speech finishes.

And I don't want it to be called right away. I want it after the speech is finished. So is it possible? Any method is welcomed. (but not so "hacky")


Solution

  • In the same page, it mentions onEvent (as part of options):

    onEvent ( optional function )
      This function is called with events that occur in the process of speaking the utterance.
      Parameters
      event ( TtsEvent )
        The update event from the text-to-speech engine indicating the status of this utterance.

    TtsEvent has a type property, which can be end to mark the speech ending. Put it together:

    chrome.tts.speak("Hello, world!", {
        requiredEventTypes: ['end'],
        onEvent: function(event) {
            if(event.type === 'end') {
                alert('Speech ended.');
            }
        }
    });