tts.js
1.49 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var gb_tts = (function() {
var storage = window.localStorage;
var defaultConfig = {
//发音速度 1 ~ 10
rate: 1.2,
//播放队列 1:覆盖式(总是播放最新) -1:完整的按顺序播报
queueModel: 1,
enable: 1
};
var readLocal = function() {
//从本地客户端读取配置信息
var cofig = storage.getItem('tts_cofig');
if (cofig) {
cofig = JSON.parse(cofig);
defaultConfig = cofig;
}
};
var writeConfig = function(newConfig) {
storage.setItem('tts_cofig', JSON.stringify(newConfig));
defaultConfig = newConfig;
};
var synth = window.speechSynthesis;
readLocal();
var speak = function(t) {
if (defaultConfig.enable != 1)
return;
if (defaultConfig.queueModel == 1)
synth.cancel();
//延迟100毫秒,防止中断旧语音时 将新的语音也中断
setTimeout(function() {
var msg = new SpeechSynthesisUtterance(t);
msg.rate = defaultConfig.rate;
synth.speak(msg);
}, 100);
};
var audition = function(t, rate) {
var msg = new SpeechSynthesisUtterance(t);
msg.rate = rate;
synth.speak(msg);
};
return {
readLocal: readLocal,
writeConfig: writeConfig,
defaultConfig: function() {
return defaultConfig
},
speak: speak,
audition: audition
};
})();