From c2ed5f5cc531d6823b6d59ef03ee816c46876aeb Mon Sep 17 00:00:00 2001 From: LiJun <LiJun@192.168.10.18> Date: 星期二, 30 十月 2018 10:49:42 +0800 Subject: [PATCH] 修复语音播报不能持续播报bug --- gx_tieta/WebRoot/js/VoiceUtil.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 47 insertions(+), 4 deletions(-) diff --git a/gx_tieta/WebRoot/js/VoiceUtil.js b/gx_tieta/WebRoot/js/VoiceUtil.js index a69bf9c..d2b3d56 100644 --- a/gx_tieta/WebRoot/js/VoiceUtil.js +++ b/gx_tieta/WebRoot/js/VoiceUtil.js @@ -3,26 +3,66 @@ this.speak = window.speechSynthesis; this.voice = ''; this.zh_CN = false; - this._setLang(); // 鑾峰彇骞舵娴嬫槸鍚︽湁涓枃鐨勭幆澧� + this.rate = 1.5; + this._setLang(); // 鑾峰彇骞舵娴嬫槸鍚︽湁涓枃鐨勭幆澧� + this.timeInterval = 28; //寰幆闂撮殧(绉�) + this.speak_enable = true; //ture:寮�鍚闊虫挱鎶� false:鍋滄璇煶鎾姤 }; // 鎾姤璇煶 (璋冪敤鍙戝0鐨勬柟娉曟椂璇蜂娇鐢ㄥ欢鏃跺彂澹�) -Voice.prototype.play = function(txt) { +Voice.prototype.play = function(txt,callback) { if(!this.zh_CN) { console.info('浣犵殑鐢佃剳涓嶆敮鎸佷腑鏂囨挱鎶ワ紒'); } - console.info(this.zh_CN); + _timeInterval = this.timeInterval; + //console.info(this.speak_enable); + if(!this.speak_enable){ //璇嗗埆鏄惁寮�鍚0闊虫挱鎶� + if(callback && typeof callback == 'function'){ + setTimeout(function(){ + callback(); + }, Math.abs(_timeInterval)*1000); + } + return; + } this.cancel(); var to_speak = new SpeechSynthesisUtterance(txt); to_speak.voice = this.voice; // 璁惧畾涓枃鎾姤 - + to_speak.rate = this.rate; + //console.info(to_speak); + var starttime = new Date(); this.speak.speak(to_speak); + console.info(to_speak); + to_speak.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); + } + } + }; + to_speak.onerror = function(event) { + console.info(event); + }; }; // 閫�鍑哄綋鍓嶆挱鎶� Voice.prototype.cancel = function() { this.speak.cancel(); +}; + + +//鍋滄璇煶鎾姤 +Voice.prototype.stopSpeak = function() { + this.speak.cancel(); + this.speak_enable = false; }; // 妫�鏌ュ綋鍓嶈瑷�鐜 @@ -41,4 +81,7 @@ }, 0); }; + + var voice = new Voice(); +voice.cancel(); \ No newline at end of file -- Gitblit v1.9.1