Search code examples
javascripthtmlapispeech-synthesis

speechSynthesis API example gives error


The example given on Web Speech API Specification

    speechSynthesis.speak(SpeechSynthesisUtterance('Hello World'));

gives the following error on chrome:

Uncaught TypeError: DOM object constructor cannot be called as a function.

Can anyone help here?

Thanks!


Solution

  • I think there is a type in the specifications and you are expected to use the new keyword with the SpeechSynthesisUtterance object. Try this:

    speechSynthesis.speak(new SpeechSynthesisUtterance('Hello World'));