Javascript onoff按钮在chatbot中不起作用

时间:2019-06-23 09:26:24

标签: javascript text-to-speech chatbot

我是Java语言的新手,我想使用语音识别和文本到语音的网站构建一个聊天机器人。我做了开/关按钮,但是它不起作用。

我尝试过:

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 个答案:

答案 0 :(得分:1)

为此,有window.speechSynthesis的暂停和恢复方法;

要停止:

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

请参阅:pause

要继续:

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

请参阅resume

取消(停止)

speechSynthesisInstance.cancel();

请参阅cancel

根据您的条件,您可以调用这些方法

有关其他方法,请参见here

更新:-

这是粗略的代码,请根据您的要求进行更改

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 -sample project