將文字換成 聲音 demo

<html>
<head>
<title>文字轉聲音</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
<meta name="description"
    content="Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description.
">

</head>
<body>
    <div>
    <div> 文字轉聲音</div>
    <div>
        <input type="text" id= "words" />
        <input type="button" οnclick="speakText()" value="開始讀"/>
        <input type="button" οnclick="stopSpeak()" value="停止讀"/>
    </div>
    </div>
    
    <script type="text/javascript">
        var speaker = new window.SpeechSynthesisUtterance();
        var speakTimer,
            stopTimer;

        // 開始朗讀
        function speakText() {
            var text = document.getElementById("words").value;
            if(text == ""){
                text = "你還沒有輸入 要讀的內容"
            }
            clearTimeout(speakTimer);
            window.speechSynthesis.cancel();
            speakTimer = setTimeout(function () {
                speaker.text = text;
                window.speechSynthesis.speak(speaker);
            }, 200);
        }

        // 停止朗讀
        function stopSpeak() {
            clearTimeout(stopTimer);
            clearTimeout(speakTimer);
            stopTimer = setTimeout(function () {
                window.speechSynthesis.cancel();
            }, 20);
        }
    </script>
</body>
</html>

 

創建一個 html 格式的文件,然後用 notepad 打開,將上面貼上就ok了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章