Кнопка включения Javascript не работает в чате - PullRequest
0 голосов
/ 23 июня 2019

Я полностью новичок в Javascript, и я хотел бы создать чат-бота, используя распознавание голоса и преобразование текста в речь для веб-сайта. Я сделал кнопку включения, но она не работает.

Я пробовал:

while (value == "on") { 
  utterance.volume = 1; 
}

также

if (value == "on") {
  utterance.volume = 1;
} else {
  utterance.volume = 0;
}

но все же что-то не так. Любая помощь

function onoff(){
    currentvalue = document.getElementById('onoff').value;
    if(currentvalue == "on"){
        document.getElementById("onoff").value="on";
    }else{
          document.getElementById("onoff").value="off";
    }
 return currentvalue;
}



function speak(string){
var soundonoff = onoff();
    var utterance = new SpeechSynthesisUtterance();
    utterance.voice = speechSynthesis.getVoices().filter(function(voice) 
    {return voice.name == "Alex";})[0];
    utterance.text = string;
    utterance.lang = "en-US"; 
    if (soundonoff == "on"){
    utterance.volume = 1; //0-1 interval
    }else{
    terance.volume = 0;
    }
    utterance.rate = 0.8;
    utterance.pitch = 1; //0-2 interval
    speechSynthesis.speak(utterance);

}

1 Ответ

1 голос
/ 23 июня 2019

для этого есть метод паузы и возобновления для window.speechSynthesis;

Для остановки:

speechSynthesis.pause(); // pauses utterances being spoken

см .: пауза

ДляРезюме:

  speechSynthesis.resume();  /// resumes utters ,

см. резюме

отменить (остановить)

speechSynthesisInstance.cancel();

см. отменить

в соответствии с вашими условиями, вы можете вызвать эти методы

, другие методы см. здесь

ОБНОВЛЕНИЕ: -

это грубый код, измените в соответствии с вашими требованиями

HTML:

<input type="checkbox" id="onoff" name="onoff" >On/Off<br>
<br/>
<input type="button" name="start" onclick="speak();" value ="start" > On/Off
 <br>

Сценарий:

 <script>

  var synth = window.speechSynthesis;

   function speak(){
      var utterThis = new SpeechSynthesisUtterance('My pleasure, poke me up if you need more info.'); // 
      synth.speak(utterThis);
    }


 //// on check box change event call this 

   $('#onoff').change(function() {

      if($("#onoff").prop('checked') == true){
       speechSynthesis.pause();
      /// now I dont know you want to pause or stop, change it according to you 
       ///requirment 
       }  
     else {
         speechSynthesis.pause();
       }
  });
  </script>

GITHUB - пример проекта

...