bug
D:/workspace/chenjingjing/git/gx_tieta/gx_tieta/.gitignore
2019-01-18 8fce91f5d14701f2eb058302b9f10d9e681a81da
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
<!DOCTYPE html>
<html>
 
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>H5语音播报功能</title>
        <style>
            article {margin: 0 auto;max-width: 800px;text-align: center;}
            textarea {max-width: 600px;width:100%;text-align: left;}
            button{border-radius: 3px;border: 1px solid #dddddd;height: 30px;width: 80px;cursor: pointer;}
        </style>
        <link rel="stylesheet" href="src/css/layui.css">
    </head>
 
    <body>
        我是语音播报
    </body>
    <script type="text/javascript" src="src/layui.all.js"></script>
    <script>
        
        // 设置语音播报对象
        var Voice = function() {
            this.speak = window.speechSynthesis;
            this.voice = '';
            this.zh_CN = false;
            this._setLang();    // 获取并检测是否有中文的环境
        };
 
        // 播报语音
        Voice.prototype.play = function(txt) {
            if(!this.zh_CN) {
                if(layer) {
                    layer.msg('你的电脑不支持中文播报!');
                }else {
                    alert('你的电脑不支持中文播报!');
                }
                return;
            }
            this.cancel();
            var to_speak = new SpeechSynthesisUtterance(txt);
            to_speak.voice = this.voice;    // 设定中文播报
 
            this.speak.speak(to_speak);
            
        };
        
        // 退出当前播报
        Voice.prototype.cancel = function() {
            this.speak.cancel();
        };
 
        // 检查当前语言环境
        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];
                    if(_voices.lang === 'zh-CN') {
                        _this.zh_CN = true;
                        _this.voice = _voices;
                    }
                }
            }, 0);
        };
        
 
        var voice = new Voice();
        
        setTimeout(function() {
            voice.play('我是语音播报');
        }, 3000);
    </script>
 
</html>