File size: 2,927 Bytes
50408e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e06f9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50408e8
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


function random(max_num) {
  return Math.floor(Math.random() * (max_num - 0 + 1) + 0);
}

function downInit() {
  // if(score >)
  setInterval(downOne, 5000);
}
function downOne() {
  if(document.getElementById("score").innerHTML === '')
    score = 0
  else
    score = parseInt(document.getElementById("score").innerHTML);
  if(Object.is(score,NaN)) score = 0;
  
  if(score && score > max_score*0.5) {
    score = Math.max(0,score - 1);
    document.getElementById("score").innerHTML = score;
  }
  

}
function resultImg(result){
  if(result === '正确')
    return '<img src="images/right.png" width="45" height="45" align="middle"/>';
  return '<img src="images/error.png" width="45" height="45" align="middle"/>';

}
// 全局声明 audio
let audio = null;
let max_score = 200;




function ttsStop(params) {
  if(audio!= null)
    localStorage.setItem('audioEnded', true);
}
// 实时获取后台返回的 音频流(MP3流)并进行播放
function ttsPlay(params) {
  window.onbeforeunload=function(e){     
    ttsStop()
    // var e = window.event||e;  
    // e.returnValue=("确定离开当前页面吗?");
  }
  return new Promise((resolve, reject) => {
    axios({
      method: 'post',
      url: 'https://tsn.baidu.com/text2audio?lan=zh&per=4121&cuid=baidu_speech_demo&idx=1&cod=2&lan=zh&ctp=1&pdt=220&aue=3&pit=5&ie=UTF-8&spd=2&tex=' + params,
      responseType: 'arraybuffer'
    }).then((response) => {
      // 将 blob 数据转换成 url
      let mp3Url = window.URL.createObjectURL(new Blob([response.data]))

      // 进行音频播放
      try {
        var playCount = 30;
        //是否已经声明过
        if (audio == null) {
          audio = new Audio();
          audio.addEventListener('ended', function() {
            // alert(playCount)
            // playCount = playCount - 1;
            // if(playCount <= 0)
            //   localStorage.setItem('audioEnded', true);
            // else{
            setTimeout("audio.play()",1000)
              
            // }
          }, false);
        }
        if (mp3Url) {
          audio.src = mp3Url;
          // audio.multer = true;
          audio.play();
        }
      } catch (e) {}
    }).catch((error) => {
      reject(error);
    })
  })
}


function readTxt(data) { 
      var lines = data.split("\n"); //按行读取
  
      var list = []
      $.each(lines,function(i, v) {
          // console.log(v);
          if( v.trim()!= '')
            list.push(v)
      });
      return list;
  
}

function readLocalTxt(fileName) {
  var reader = new FileReader();
  var file = new File(fileName=fileName); //files为上传组件获取的地址
  reader.readAsText(file, 'utf-8');
  reader.onload = function(){
      reader.result.split('\n').forEach(function(v, i){
          console.log(v);
      });
  };
  reader.onerror = function(){
      console.log('读取失败');
      console.log(reader.error);
  };
}