js实现语音朗读

js实现语音朗读

文章原文

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<!doctype html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport'
content='width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0'>
<meta http-equiv='X-UA-Compatible' content='ie=edge'>
<title>测试语音合成</title>
</head>
<body>
<button id='play'>播放</button>
<button id='stop'>停止</button>
<script>
const speaker = new window.SpeechSynthesisUtterance()

// 播放语音
function speak(text) {
speaker.text = text
window.speechSynthesis.speak(speaker)
}

// 停止语音
function stop() {
window.speechSynthesis.cancel()
}

document.querySelector('#play').onclick = () => speak('编号是:1234567890123415413512341')
document.querySelector('#stop').onclick = stop

</script>
</body>
</html>