1

This simple code works fine when I click "Play" button:

<html>
    <head>
        <script src="https://code.responsivevoice.org/responsivevoice.js"></script>
    </head>
    <body>


    <input 
      onclick="responsiveVoice.speak('Lily, you can not just freeze me out like this.','US English Female'); responsiveVoice.speak('love you.','US 

English Female');" 
      type="button" 
      value="Play" 
        />

    </body>

</html>

However, when I tried to put it into a function called on load, it didn't work:

<html>
    <head>
    <script src="https://code.responsivevoice.org/responsivevoice.js"></script>
    <script type="text/javascript"> 
        function read(){

            responsiveVoice.speak('Lily, you can not just freeze me out like this.','US English Female');
        responsiveVoice.speak('Love you.','US English Female');
        }
    </script>
</head>

<body onload="javascript:read();">
</body>

</html>

Do you know what the problem could be?

Naftali
  • 144,921
  • 39
  • 244
  • 303
Tum
  • 3,614
  • 5
  • 38
  • 63

3 Answers3

3

If you look at the responsive voice code they have a weird ~200 millisecond timeout in their code:

        a.enableFallbackMode()) : setTimeout(function() {
            var b = setInterval(function() {
                var c = window.speechSynthesis.getVoices();
                0 != c.length || null  != a.systemvoices && 
                0 != a.systemvoices.length ? (console.log("RV: Voice support ready"),
                a.systemVoicesReady(c),
                clearInterval(b)) : (console.log("Voice support NOT ready"),
                a.voicesupport_attempts++,
                a.voicesupport_attempts > a.VOICESUPPORT_ATTEMPTLIMIT && (clearInterval(b),
                null  != window.speechSynthesis ? a.iOS ? (a.iOS9 ? a.systemVoicesReady(a.cache_ios9_voices) : a.systemVoicesReady(a.cache_ios_voices),
                console.log("RV: Voice support ready (cached)")) : (console.log("RV: speechSynthesis present but no system voices found"),
                a.enableFallbackMode()) : 
                a.enableFallbackMode()))
            }, 100)
        }, 100);
        a.Dispatch("OnLoad")

If you try to use a voice before the timeout is up you will hit this console log:

1570RV: ERROR: No voice found for: US English Female

Which in my experience is bad and should probably be throwing an error.


If you want to keep using this script the only solution is to wait at least 201 ms in order to wait for all of the voices to load (but you only have to do this once)

let readItOnce = false;
function read(){
    const readIt = () => {
        readItOnce = true;
        responsiveVoice.speak('Lily, you can not just freeze me out like this.','US English Female');
        responsiveVoice.speak('Love you.','US English Female');
    }
    if (!readItOnce) { setTimeout(readIt, 201)}
    else { readIt(); }
}

Also do what is suggested here: https://stackoverflow.com/a/36654326/561731 in the onload of the function.

<html>
<head>
<script src="https://code.responsivevoice.org/responsivevoice.js"></script>
<script type="text/javascript"> 
let readItOnce = false;
function read(){
    const readIt = () => {
        readItOnce = true;
        responsiveVoice.speak('Lily, you can not just freeze me out like this.','US English Female');
        responsiveVoice.speak('Love you.','US English Female');
    }
    if (!readItOnce) { setTimeout(readIt, 201)}
    else { readIt(); }
}
</script>
</head>

<body onload="read();">
</body>

</html>
Community
  • 1
  • 1
Naftali
  • 144,921
  • 39
  • 244
  • 303
  • 3
    It's horrifying (i.e. should anybody trust these developers?) that this seems to be the only officially-supported way of waiting for this library to be ready. I see from that code you quoted that they have an `OnLoad` event that you can listen to... but they don't seem to have any properties you can (officially) check to determine whether it has loaded or not, so there's no way to know if you need to wait for the event! Crazy! (I'd really prefer a `.ready` Promise if I could choose, but I don't know if that aligns with their platform requirements.) – Jeremy Apr 15 '16 at 18:45
  • 1
    Yea... @JeremyBanks there are no errors or anything thrown in their code... so it is quite hard to debug unless you just search for the console logs... (which is very error prone) – Naftali Apr 15 '16 at 18:46
  • 1
    Yea and their `OnLoad` event happened **before** the timeout is done... @JeremyBanks makes no sense... – Naftali Apr 15 '16 at 18:47
1

Incorrect:

<body onload="javascript:read();">

vs

Correct:

<body onload="read();">
Community
  • 1
  • 1
Naftali
  • 144,921
  • 39
  • 244
  • 303
  • On my browser, it works fine with or without the javascript protocol. The only issue is that it only plays the last requested voice. Which is what I'd expect. – ManoDestra Apr 15 '16 at 18:28
  • DOM hasn't fully loaded the lib – Jeff Puckett Apr 15 '16 at 18:28
  • @JeffPuckettII that should not matter. the script is loaded in the head tag. and the code is not dependent on the DOM – Naftali Apr 15 '16 at 18:29
  • The script is in the head tag, so it has been preloaded. – ManoDestra Apr 15 '16 at 18:30
  • it shouldn't, this is the fringe of my knowledge, but I can just see in the developers console log that it's not ready. maybe there's some additional initialization that has to happen with it. – Jeff Puckett Apr 15 '16 at 18:30
1

just wait a second for it to load, as they suggest

<html>
<head>
<script src="https://code.responsivevoice.org/responsivevoice.js"></script>
<script type="text/javascript"> 
function read(){

    responsiveVoice.speak('Lily, you can not just freeze me out like this.','US English Female');
    responsiveVoice.speak('Love you.','US English Female');
}
setTimeout(function(){ read(); }, 1000);
</script>
</head>

<body>
</body>

</html>
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • 1
    If you look at the console (if you put it it in the onload event) it just says `RV: ERROR: No voice found for: US English Female` So the code did load, there is just some issue in the speaker code. – Naftali Apr 15 '16 at 18:33
  • 1
    even [their documentation](http://responsivevoice.org/text-to-speech-sdk/automatically-speak-text-when-webpage-displays/) suggests to set a timeout – Jeff Puckett Apr 15 '16 at 18:37