let temporaryVariable_preText = ""; // 上次播报的语音,不充分播报 let temporaryVariable_isAudio = true; // 播放状态,是否开启 // 防抖函数 function debounce(func, wait) { let timeout; return function (...args) { const context = this; clearTimeout(timeout); timeout = setTimeout(() => func.apply(context, args), wait); return () => clearTimeout(timeout); // 返回一个取消函数 }; } // 语音播报 播报dom的内容 function speechDomText(event) { // console.log("speechDomText", event); event.stopPropagation(); let str = ""; if ( event && event.target ) { let nodeName = event.target.nodeName if (nodeName == "BUTTON" || nodeName == "DIV") { str = event.target.innerText; } } if (str && str !== "") { voiceBroadcast(str); } } // 使用防抖函数包装 speechDomText const debouncedSpeechDomText = debounce(speechDomText, 500); function speechDomTextBarrier(event) { // console.log("speechDomTextBarrier", event); event.stopPropagation(); let str = ""; if ( event && event.target && (event.target.ariaLabel || event.target.ariaDescription) ) { str = event.target.ariaLabel || event.target.ariaDescription; } if (str && str !== "") { voiceBroadcast(str); } } // 使用防抖函数包装 speechDomTextBarrier,增加防抖时间 const debouncedSpeechDomTextBarrier = debounce(speechDomTextBarrier, 2000); // 游戏专用的语音播报函数,增加更长的防抖时间 function speechDomTextBarrierGame(event) { // console.log("speechDomTextBarrierGame", event); event.stopPropagation(); let str = ""; if ( event && event.target && (event.target.ariaLabel || event.target.ariaDescription) ) { str = event.target.ariaLabel || event.target.ariaDescription; } if (str && str !== "") { // 检查虚拟老师是否正在播放语音,如果是则跳过播报 if (window.virtualTeacherPlaying || (window.virtualTeacher && window.virtualTeacher.isPlaying)) { console.log('虚拟老师正在播放语音,跳过游戏名称播报:', str); return; } voiceBroadcast(str); } } // 使用更长的防抖时间包装游戏语音播报 const debouncedSpeechDomTextBarrierGame = debounce(speechDomTextBarrierGame, 3000); // 设置是否语音播报 function setIsAudio(event) { temporaryVariable_isAudio = !temporaryVariable_isAudio; event.target.innerText = temporaryVariable_isAudio ? "📢" : "🔇"; voiceBroadcast(temporaryVariable_isAudio ? "开启语音播报" : "关闭语音播报"); } // 调用讯飞接口播报语音 function voiceBroadcast(text) { if (temporaryVariable_preText == text || !temporaryVariable_isAudio) { return } // 检查虚拟老师是否正在播放语音,如果是则跳过播报 if (window.virtualTeacherPlaying || (window.virtualTeacher && window.virtualTeacher.isPlaying)) { console.log('虚拟老师正在播放语音,跳过无障碍播报:', text); return; } temporaryVariable_preText = text; const audioPlayer = document.getElementById('audioPlayer'); const vcn = ["xiaoyan", "aisjiuxu", "aisxping", "aisjinger", "aisbabyxu", "x4_lingxiaoqi_cts", "x4_lingyouyou", "x4_lingfeizhe_zl"]; $.ajax({ url: 'http://rgclass.iflysse.com:8200/text2audio', type: 'POST', contentType: 'application/x-www-form-urlencoded', data: { text: text, vcn: vcn[6], speed: 50 }, headers: { 'Cache-Control': 'no-store' }, success: function (data) { let timer01 = setTimeout(function () { const url = `http://rgclass.iflysse.com:8200${data}`; audioPlayer.src = url; audioPlayer.load(); audioPlayer.play(); clearTimeout(timer01); // console.log("text2audio success", url, audioPlayer); }, 800); }, error: function (jqXHR, textStatus, errorThrown) { console.error('text2audio error:', textStatus, errorThrown); } }); let timer02 = setTimeout(function () { temporaryVariable_preText = ""; clearTimeout(timer02); }, 5000); }