js实现语音朗读
发布: 2018-06-18 23:10:28标签: 前端开发
js实现语音朗读
01<!doctype html>02<html lang='en'>03<head>04 <meta charset='UTF-8'>05 <meta name='viewport'06 content='width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0'>07 <meta http-equiv='X-UA-Compatible' content='ie=edge'>08 <title>测试语音合成</title>09</head>10<body>11<button id='play'>播放</button>12<button id='stop'>停止</button>13<script>14 const speaker = new window.SpeechSynthesisUtterance()1516 // 播放语音17 function speak(text) {18 speaker.text = text19 window.speechSynthesis.speak(speaker)20 }2122 // 停止语音23 function stop() {24 window.speechSynthesis.cancel()25 }2627 document.querySelector('#play').onclick = () => speak('编号是:1234567890123415413512341')28 document.querySelector('#stop').onclick = stop2930</script>31</body>32</html>
复制代码