Je veux utiliser la méthode d'annulation de fenêtre.speechsishesis en chrome, pour couper une énonciation et commencer une nouvelle (vous n'avez donc pas à entendre tous les énoncés qui sont toujours en file d'attente)
var test = new SpeechSynthesisUtterance("Test"); window.speechSynthesis.speak(test); window.speechSynthesis.cancel(); var test2 = new SpeechSynthesisUtterance("Test2"); window.speechSynthesis.speak(test2);
4 Réponses :
Je viens de faire face au même problème, émettant un J'ai ajouté un petit délai d'attente (250ms) après L'appel
function texto_a_voz(reproducir, idioma) { var synth = window.speechSynthesis; var voices = []; voices = synth.getVoices(); var utterThis = new SpeechSynthesisUtterance(reproducir); utterThis.voice = voices[idioma]; utterThis.pitch = pitch.value; utterThis.rate = rate.value; if (synth.speaking) { // SpeechSyn is currently speaking, cancel the current utterance(s) synth.cancel(); setTimeout(function () { synth.speak(utterThis); }, 250); } else { // Good to go synth.speak(utterThis); } }
pris ce code directement sur les exemples du P5Speech Lib. Peut-être que cela aide comme un travail autour?
function parseResult() { // recognition system will often append words into phrases. // so hack here is to only use the last word: var mostrecentword = myRec.resultString.split(' ').pop(); if(mostrecentword.indexOf("left")!==-1) { dx=-1;dy=0; } else if(mostrecentword.indexOf("right")!==-1) { dx=1;dy=0; } else if(mostrecentword.indexOf("up")!==-1) { dx=0;dy=-1; } else if(mostrecentword.indexOf("down")!==-1) { dx=0;dy=1; } else if(mostrecentword.indexOf("clear")!==-1) { background(255); } console.log(mostrecentword); }
Il semble fonctionner maintenant, en utilisant le code que vous avez fourni.
p>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="speak">CLICK ME TO HEAR TEXT</div>
Confirmé. J'entends "Test2" dans Chrome 76 / Windows 7.