JS调用google DEMO朗读

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <script src="js/jquery-1.8.3.js"></script>
    <title>中文测试</title>
</head>
<body>
<button id="a">开始</button>
<button id="b">暂停</button>
<button id="c">继续</button>
<button id="d">停止</button>
</body>
</html>
<script>
    var synth = window.speechSynthesis
    var u = new SpeechSynthesisUtterance();
    //汉语
    u.lang = 'zh-CN';
    u.rate = 1;
    function speak(textToSpeak) {
        u.text = textToSpeak;
        synth.speak(u)
    }
    var str = "众所周知,古诗中有好多字音与我们现代的音有所不同,如果是细心研究过过学习过还好,但有时遇到模棱两可的字音,真的不知道读什么好,读起来也没有底气"
    speak(str)
    $("#a").click(function() {
        speak(str)
    })
    $("#b").click(function() {
        synth.pause()
    })
    $("#c").click(function() {
        synth.resume()
    })
    $("#d").click(function() {
        synth.cancel();
    })
    // var synth = window.speechSynthesis;
    // var utterance1 = new SpeechSynthesisUtterance('How about we say this now? This is quite a long sentence to say.');
    // var utterance2 = new SpeechSynthesisUtterance('We should say another sentence too, just to be on the safe side.');
    // synth.speak(utterance1);
    // synth.speak(utterance2);
    // synth.pause(); // pauses utterances being spoken
    //https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesis/cancel
</script>

你可能感兴趣的:(javascript)