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()
15
16 // 播放语音
17 function speak(text) {
18 speaker.text = text
19 window.speechSynthesis.speak(speaker)
20 }
21
22 // 停止语音
23 function stop() {
24 window.speechSynthesis.cancel()
25 }
26
27 document.querySelector('#play').onclick = () => speak('编号是:1234567890123415413512341')
28 document.querySelector('#stop').onclick = stop
29
30</script>
31</body>
32</html>
复制代码