我需要帮助在一行代码中更改按钮的功能。 [英] I need help changing the function of a button in a line of code.

查看:61
本文介绍了我需要帮助在一行代码中更改按钮的功能。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个人工智能操作系统,但我想将按钮功能从启动语音识别更改为只需输入现场提供的文本。



I have an artificial intelligence operating system, but I would like to change the button function from initiating voice recognition to just simply entering the text that is provided in the field.

<title>Barry
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  
  
    var accessToken = "dd5d6938ace84f01bbfc2d8209257b06",
      baseUrl = "https://api.api.ai/v1/",
      $speechInput,
      $recBtn,
      recognition,
      messageRecording = "Recording...",
      messageCouldntHear = "I couldn't hear you, could you say that again?",
      messageInternalError = "Oh no, there has been an internal server error",
      messageSorry = "I'm sorry, I don't have the answer to that yet.";
    $(document).ready(function() {
      $speechInput = $("#speech");
      $recBtn = $("#rec");
      $speechInput.keypress(function(event) {
        if (event.which == 13) {
          event.preventDefault();
          send();
        }
      });
      $recBtn.on("click", function(event) {
        switchRecognition();
      });
      $(".debug__btn").on("click", function() {
        $(this).next().toggleClass("is-active");
        return false;
      });
    });
    function startRecognition() {
      recognition = new webkitSpeechRecognition();
      recognition.continuous = false;
          recognition.interimResults = false;
      recognition.onstart = function(event) {
        respond(messageRecording);
        updateRec();
      };
      recognition.onresult = function(event) {
        recognition.onend = null;
        
        var text = "";
          for (var i = event.resultIndex; i &lt; event.results.length; ++i) {
            text += event.results[i][0].transcript;
          }
          setInput(text);
        stopRecognition();
      };
      recognition.onend = function() {
        respond(messageCouldntHear);
        stopRecognition();
      };
      recognition.lang = "en-US";
      recognition.start();
    }
  
    function stopRecognition() {
      if (recognition) {
        recognition.stop();
        recognition = null;
      }
      updateRec();
    }
    function switchRecognition() {
      if (recognition) {
        stopRecognition();
      } else {
        startRecognition();
      }
    }
    function setInput(text) {
      $speechInput.val(text);
      send();
    }
    function updateRec() {
      $recBtn.text(recognition ? "Stop" : "Speak");
    }
    function send() {
      var text = $speechInput.val();
      $.ajax({
        type: "POST",
        url: baseUrl + "query",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        headers: {
          "Authorization": "Bearer " + accessToken
        },
        data: JSON.stringify({query: text, lang: "en", sessionId: "yaydevdiner"}),
        success: function(data) {
          prepareResponse(data);
        },
        error: function() {
          respond(messageInternalError);
        }
      });
    }
    function prepareResponse(val) {
      var debugJSON = JSON.stringify(val, undefined, 2),
        spokenResponse = val.result.speech;
      respond(spokenResponse);
      debugRespond(debugJSON);
    }
    function debugRespond(val) {
      $("#response").text(val);
    }
    function respond(val) {
      if (val == "") {
        val = messageSorry;
      }
      if (val !== messageRecording) {
        var msg = new SpeechSynthesisUtterance();
        msg.voiceURI = "native";
        msg.text = val;
        msg.lang = "en-US";
        window.speechSynthesis.speak(msg);
      }
      $("#spokenResponse").addClass("is-active").find(".spoken-response__text").html(val);
    }
  
  
    html {
      box-sizing: border-box;
    }
    *, *:before, *:after {
      box-sizing: inherit;
    }
    body {
     ;
      font-family: "Titillium Web", Arial, sans-serif;
      font-size: 20px;
      margin: 0;
    }
    .container {
      position: fixed;
      top: 50%;
      left: 50%;
      -webkit-transform: translate(-50%, -50%);
    }
    input {
      background-color: #ffffff;
      border: 2px solid #307128;
      color: #307128;
      font-family: "Titillium Web";
      font-size: 20px;
      line-height: 43px;
      padding: 0 0.75em;
      width: 400px;
      -webkit-transition: all 0.35s ease-in;
    }
    textarea {
      background-color: #ffffff;
      border: 2px solid #307128;
      color: #307128;
      padding: 0.5em;
      width: 100%;
      -webkit-transition: all 0.35s ease-in;
    }
    input:active, input:focus, textarea:active, textarea:focus {
      outline: 1px solid #307128;
    }
    .btn {
      background-color: #ffffff;
      border: 2px solid #307128;
      color: #307128;
      cursor: pointer;
      display: inline-block;
      font-family: "Titillium Web";
      font-size: 20px;
      line-height: 43px;
      padding: 0 0.75em;
      text-align: center;
      text-transform: uppercase;
      -webkit-transition: all 0.35s ease-in;
    }
    .btn:hover {
      background-color: #307128;
      color: #ffffff;
    }
    .debug {
      bottom: 0;
      position: fixed;
      right: 0;
    }
    .debug__content {
      font-size: 14px;
      max-height: 0;
      overflow: hidden;
      -webkit-transition: all 0.35s ease-in;
    }
    .debug__content.is-active {
      display: block;
      max-height: 500px;
    }
    .debug__btn {
      width: 100%;
    }
    .spoken-response {
      max-height: 0;
      overflow: hidden;
      -webkit-transition: all 0.35s ease-in;
    }
    .spoken-response.is-active {
      max-height: 400px;
    }
    .spoken-response__text {
      background-color: #ffffff;
      color: #307128;
      padding: 1em;
    }
  


  <div class="container">
    
    Speak
    <div id="spokenResponse" class="spoken-response">
      <div class="spoken-response__text"></div>
    </div>
  </div>





我尝试了什么:



我没有尝试过任何东西因为我不知道怎么做更改它。



What I have tried:

I haven't tried anything because I don't know how to change it.

推荐答案

speechInput,
speechInput,


recBtn,
识别,
messageRecording =录制...,
messageCouldntHear =我听不到你,你能再说一次吗?,
messageInternalError =哦不,内部服务器出错,
messageSorry =对不起,我不知道还没有答案。;
recBtn, recognition, messageRecording = "Recording...", messageCouldntHear = "I couldn't hear you, could you say that again?", messageInternalError = "Oh no, there has been an internal server error", messageSorry = "I'm sorry, I don't have the answer to that yet.";


(document).ready(function(){
(document).ready(function() {


这篇关于我需要帮助在一行代码中更改按钮的功能。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆