Text To Speech

I’m trying out text to speech using window.speechSynthesis.getVoices(). Please look at my code here. You will see that all I have is a button on the page and it’s supposed to say “Test!” when the button is clicked but it’s not working the first time the button is clicked. The button needs to be clicked several times for it to work. Also this works in Chrome but not work Firefox.

Hi there oangodd,

I can’t comment on your fiddle - I dont do jQuery. :winky:

I do, though, have a Vanilla example here…

Text to speech tool

coothead

1 Like

Hi,
The code for text to speech is window.speechSynthesis.getVoices() which is native Javascript and not jQuery. I only made sure the page is loaded when I called getVoices() using the jQuery document.ready() function. Other than that and the click event listener, everything else is just vanilla javascript.

Hi there oangodd,

I can tell you that console.log( voices.length )
…shows 1 in Firefox and 20 in Chrome. :eek:

Also note:-

This is an experimental technology
Check the Browser compatibility table
carefully before using this in production.

source:-

MDN - SpeechSynthesis.getVoices()

coothead

The Button is working the first time it just takes time to do all the background jQuery processing.

Hi John,
The line var voices = window.speechSynthesis.getVoices() returns an array of objects called SpeechSynthesisVoice. Each SpeechSynthesisVoice object contains the properties default, lang, localService, name, and voiceURI. I think that because there are several of these objects it takes several seconds to load them.

I tried to cut down the load time by putting only the object with property values I’m looking for in an array and tried to use this object instead of calling the getVoices method but it is not working. Is this even possible, can I create my own SpeechSynthesisVoice object ?

I have just copied and pasted the following Wiki page contents to @coothead’s example and speech started immediately not only but also the speech is currently playing in the background and has never once hicupped or stuttered :slight_smile:

Perhaps offer a small fee and ask @coothead for the source code.

Edit:

I just noticed the source code is available to be downloaded.