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
| const synth = window.speechSynthesis; if(!synth){ alert("浏览器不支持阅读") } const sel = document.querySelectorAll("#sel")[0] let voices let selectActive getVoices().then(res=>{ voices = res for(v of res){ var option = document.createElement("option"); option.text = v.name option.value = v.name if(v.name.includes("Yunxi ")){ option.selected = true; selectActive = v.name } document.querySelectorAll("#sel")[0].add(option); } })
sel.addEventListener('change',function(e){ console.log(this,this.value); selectActive = this.value })
document.querySelectorAll("#pause")[0].addEventListener("click",function(){ synth.pause() })
document.querySelectorAll("#resume")[0].addEventListener("click",function(){ synth.resume() })
function readTextPlay(){ let txt = document.querySelectorAll("#text")[0].value let readText = new SpeechSynthesisUtterance(txt.trim()) readText.onend = function (event) { console.log("阅读完成"); getDatas() }; readText.onerror = function (event) { console.error("阅读出错"); }; readText.voice = voices.filter(v=>v.name == selectActive)[0]; readText.pitch = 1; readText.rate = 1; synth.speak(readText); }
function getVoices(){ return new Promise(function(resolve,reject){ setTimeout(()=>{ let voices = synth.getVoices() resolve(voices.filter(v=>v.name.includes("Chinese"))); },100) }) }
|