Skip to content

Commit 91a204f

Browse files
author
王下邀月熊 - WxChevalier
authored
Create chromeSpeech.js
1 parent 7e5120b commit 91a204f

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

web/media/chromeSpeech.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
reading = false;
2+
timer = false;
3+
function googleSpeech(text, rate) {
4+
if (!reading) {
5+
speechSynthesis.cancel();
6+
if (timer) {
7+
clearInterval(timer);
8+
}
9+
let msg = new SpeechSynthesisUtterance();
10+
let voices = window.speechSynthesis.getVoices();
11+
msg.voice = voices[63];
12+
msg.voiceURI = 'native';
13+
msg.volume = 1; // 0 to 1
14+
msg.rate = rate; // 0.1 to 10
15+
msg.pitch = 1; //0 to 2
16+
msg.text = text;
17+
msg.lang = 'zh-CN';
18+
19+
msg.onerror = function (e) {
20+
speechSynthesis.cancel();
21+
reading = false;
22+
clearInterval(timer);
23+
};
24+
25+
msg.onpause = function (e) {
26+
};
27+
28+
msg.onboundary = function (event) {
29+
};
30+
31+
msg.onend = function (e) {
32+
speechSynthesis.cancel();
33+
reading = false;
34+
clearInterval(timer);
35+
};
36+
37+
speechSynthesis.onerror = function (e) {
38+
speechSynthesis.cancel();
39+
reading = false;
40+
clearInterval(timer);
41+
};
42+
43+
console.log(msg);
44+
speechSynthesis.speak(msg);
45+
46+
timer = setInterval(function () {
47+
if (speechSynthesis.paused) {
48+
speechSynthesis.resume();
49+
}
50+
51+
}, 100);
52+
53+
reading = true;
54+
}
55+
56+
}

0 commit comments

Comments
 (0)