1

当我单击“播放”按钮时,这个简单的代码可以正常工作:

<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>

但是,当我尝试将其放入加载时调用的函数中时,它不起作用:

<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>

你知道问题可能是什么吗?

4

3 回答 3

3

如果您查看响应式语音代码,他们的代码中有一个奇怪的 ~200 毫秒超时:

        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")

如果您在超时前尝试使用语音,您将点击此控制台日志:

1570RV:错误:未找到适合:美国英语女性的声音

根据我的经验,这很糟糕,应该会引发错误。


如果您想继续使用此脚本,唯一的解决方案是等待至少 201 毫秒以等待所有声音加载(但您只需执行一次)

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(); }
}

还要执行此处的建议:https : //stackoverflow.com/a/36654326/561731 在函数的 onload 中。

<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>

于 2016-04-15T18:38:33.050 回答
1

不正确:

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

对比

正确的:

<body onload="read();">
于 2016-04-15T18:22:43.370 回答
1

正如他们建议的那样,只需等待一秒钟即可加载

<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>

于 2016-04-15T18:28:10.467 回答