javascript - 中文文本使用 Web Speech API 播放一次,但不会播放第二次

标签 javascript webspeech-api

所以我正在使用修改后的脚本来尝试播放来自 Web Speech API 的一些文本。

代码原来在这里:

Chrome Speech Synthesis with longer texts

这是我修改后的变体:

function googleSpeech(text, rate) {
    if (!reading) {
        speechSynthesis.cancel();
        if (timer) {
            clearInterval(timer);
        }
        let msg = new SpeechSynthesisUtterance();
        let voices = window.speechSynthesis.getVoices();
        msg.voice = voices[63];
        msg.voiceURI = 'native';
        msg.volume = 1; // 0 to 1
        msg.rate = rate; // 0.1 to 10
        msg.pitch = 1; //0 to 2
        msg.text = text;
        msg.lang = 'zh-CN';

        msg.onerror = function (e) {
            speechSynthesis.cancel();
            reading = false;
            clearInterval(timer);
        };

        msg.onpause = function (e) {
        };

        msg.onboundary = function (event) {
        };

        msg.onend = function (e) {
            speechSynthesis.cancel();
            reading = false;
            clearInterval(timer);
        };

        speechSynthesis.onerror = function (e) {
            speechSynthesis.cancel();
            reading = false;
            clearInterval(timer);
        };

        console.log(msg);
        speechSynthesis.speak(msg);

        timer = setInterval(function () {
            if (speechSynthesis.paused) {
                speechSynthesis.resume();
            }

        }, 100);

        reading = true;
    }

}

我能让它播放一次。每当我尝试让它再次播放时,它都不起作用 - 尽管运行了 speechSynthesis.cancel();。当我重新加载页面时,一切正常 - 播放一次。

有什么一致的方法可以再次播放文本吗?看起来这可能与 Web Speech API 中的许多错误有关。这是在 Chrome 68 上。

这是我正在播放的文本示例:

我是一个兵。来自老百姓。 打败了日本狗强盗。 消灭了蒋匪军。

最佳答案

你的代码按原样工作,除了我必须在函数之前定义 reading = falsetimer = false

我的观察是,当您传递速率值不在 0.1 到 1o 之间时,您的函数只会被调用一次。您可能需要检查您的 rate 值。

此外,如果速率值不在指定范围内,您的 onend 事件将不会被调用。

我的系统是 Mac,chrome 是 版本 67.0.3396.99(官方构建)(64 位)

<script type="text/javascript">
    

reading = false;
timer = false;
function googleSpeech(text, rate) {
    if (!reading) {
        speechSynthesis.cancel();
        if (timer) {
            clearInterval(timer);
        }
        let msg = new SpeechSynthesisUtterance();
        let voices = window.speechSynthesis.getVoices();
        msg.voice = voices[63];
        msg.voiceURI = 'native';
        msg.volume = 1; // 0 to 1
        msg.rate = rate; // 0.1 to 10
        msg.pitch = 1; //0 to 2
        msg.text = text;
        msg.lang = 'zh-CN';

        msg.onerror = function (e) {
            speechSynthesis.cancel();
            reading = false;
            clearInterval(timer);
        };

        msg.onpause = function (e) {
        };

        msg.onboundary = function (event) {
        };

        msg.onend = function (e) {
            speechSynthesis.cancel();
            reading = false;
            clearInterval(timer);
        };

        speechSynthesis.onerror = function (e) {
            speechSynthesis.cancel();
            reading = false;
            clearInterval(timer);
        };

        console.log(msg);
        speechSynthesis.speak(msg);

        timer = setInterval(function () {
            if (speechSynthesis.paused) {
                speechSynthesis.resume();
            }

        }, 100);

        reading = true;
    }

}
</script>

<button  onclick="googleSpeech('我是一个兵。来自老百姓。 打败了日本狗强盗。 消灭了蒋匪军。',1)"> Play </button>

关于javascript - 中文文本使用 Web Speech API 播放一次,但不会播放第二次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51662900/

相关文章:

javascript - 使用javascript OOP中的方法动态获取属性

JavaScript : console. 日志调试陷阱,识别误导性打印输出的经验法则

javascript - 从另一个函数解析函数内的 promise

javascript - 使用 javascript 或 ajax 弹出窗口显示所选选项

javascript - 使用 React.js 时未定义 Web Speech API SpeechRecognition

ios - 使用 Google 语音识别 API 构建 iOS 应用程序

speech-to-text - webkitSpeechRecognition() 如何在没有 API key 的情况下工作?

text-to-speech - SpeechSynthesis 无法用葡萄牙语说话 (pt-BR)

javascript - AngularJs 输入和日期选择器 css 不工作

javascript - HTML5 语音合成 API - 只能使用 "native"语音