server / youyou /js /main.js
silencewing's picture
Upload 93 files
50408e8
raw
history blame
2.26 kB
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);
})
})
}