1
81041
2019-06-20 ab3c4acf83f54f8449ca8664c4a2bb79bd30f297
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// 设置语音播报对象
var Voice = function() {
    this.speak = window.speechSynthesis;
    this.tospeak = new SpeechSynthesisUtterance('');
    this.voice = '';
    this.zh_CN = false;
    this.rate = 1.5; 
    this._setLang();            // 获取并检测是否有中文的环境
    this.timeInterval = 27;        //循环间隔(秒)
    this.speak_enable = true;            //ture:开启语音播报         false:停止语音播报
};
 
// 播报语音        (调用发声的方法时请使用延时发声)
Voice.prototype.play = function(txt,callback) {
    if(!this.zh_CN) {
        console.info('你的电脑不支持中文播报!');        
    }
    _timeInterval = this.timeInterval;
    //console.info(this.speak_enable);
    if(!this.speak_enable){                                    //识别是否开启声音播报
        if(callback && typeof callback == 'function'){
            setTimeout(function(){
                callback();
            }, Math.abs(_timeInterval)*1000);
        }
        return;
    }
    this.cancel();
    this.tospeak.text = txt;
    this.tospeak.voice = this.voice;    // 设定中文播报
    this.tospeak.rate = this.rate;
    //console.info(to_speak);
    var starttime = new Date();
    this.speak.speak(this.tospeak);
    //console.info(this.tospeak);
    this.tospeak.onend = function(event) {    
        //console.info("播放结束");
        if(callback && typeof callback == 'function'){
            var endtime = new Date();
            var timelong = (endtime.getTime()-starttime.getTime())/1000;
            //console.info(parseInt(_timeInterval)+"==="+parseInt(timelong));
            if(timelong > this.timeInterval){                
                callback();
            }else{
                setTimeout(function(){
                    callback();
                }, Math.abs(_timeInterval-timelong)*1000);
            }
        }
    };
    
 
};
 
// 开启/关闭当前语音播报 (依赖与base.js导入的Cookie方法)
Voice.prototype.changeSpeak = function(bool) {
    this.speak_enable = bool;
    setCookie('voice', bool);
    // 退出当前播报
    if(!bool) {
        this.cancel();
    }
};
 
// 退出当前播报
Voice.prototype.cancel = function() {
    this.speak.cancel();
};
 
 
//停止语音播报
Voice.prototype.stopSpeak = function() {
    this.speak.cancel();
    this.speak_enable = false;
};
 
// 检查当前语言环境
Voice.prototype._setLang = function() {
    var _this = this;
    setTimeout(function(){
        var voices = _this.speak.getVoices();
        for(var i=0; i<voices.length; i++) {
            var _voices = voices[0];
            //console.info(_voices);
            if(_voices.lang === 'zh-CN') {
                _this.zh_CN = true;
                _this.voice = _voices;
            }
        }
    }, 0);
};
 
 
 
var voice = new Voice();
var voiceStatus = checkPageVoiceStatus();
// 设置初始语音播报状态
voice.changeSpeak(voiceStatus);
voice.cancel();
 
// 设置初始化语音播报图标
if(!voiceStatus) {
    $('.web-status .cell-list.voice').addClass('close-voice');
}
 
// 点击声音的图标
$('.web-status .cell-list.voice').click(function() {
    $(this).toggleClass('close-voice');
    // 判断是否关闭声音
    if($(this).hasClass('close-voice')) {
        voice.changeSpeak(false);
    }else {
        voice.changeSpeak(true);
    }
});
 
// 获取页面语音是否开启
function checkPageVoiceStatus() {
    var voiceStatus = getCookie('voice');
    // 设置初始语音播报状态
    voiceStatus = voiceStatus=='false'?false:true;
    
    return voiceStatus;
}