javascript - 如何在页面加载后立即激活 ResponsiveVoice?

标签 javascript responsivevoice

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

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

你知道问题出在哪里吗?

最佳答案

如果您查看响应式语音代码,他们的代码中有一个奇怪的约 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: ERROR: No voice found for: US English Female

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


如果你想继续使用这个脚本,唯一的解决办法是等待至少 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在函数的加载中。

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

关于javascript - 如何在页面加载后立即激活 ResponsiveVoice?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36654291/

相关文章:

javascript - 响应式语音在 Safari 中不起作用

jquery - 响应式语音 jQuery 在 Firefox 中不起作用

javascript - 错误 : req#logout requires a callback function

javascript - 这是在 Sequelize.js 中使用 Promise 的正确方法吗?

javascript - 页面上有 'use' 标签,但我找不到 'def' 标签

javascript - 如何在70+版本中隐藏chrome视频播放器的三点按钮

javascript - jQuery - 输入时不提交数据

javascript - ResponsiveVoice.js - 绕过 iOS TTS 需要直接用户交互