Giter Site home page Giter Site logo

Comments (1)

ndeso17 avatar ndeso17 commented on September 28, 2024

here is a solution to replace "onend", but even though I got the solution, I'm still curious why "onend" doesn't work.

  const jawaban = (text) => {
    const voices = window.speechSynthesis.getVoices();
    const indonesianVoice = voices.find((voice) => voice.lang === "id-ID");
    if (indonesianVoice) {
      const utterance = new SpeechSynthesisUtterance(text);
      utterance.voice = indonesianVoice;
      window.speechSynthesis.speak(utterance);
      utterance.onend = function () {
        window.speechSynthesis.cancel();
        window.location.reload();
      };
    } else {
      console.log("Suara bahasa Indonesia tidak ditemukan.");
      const utterance = new SpeechSynthesisUtterance(text);
      window.speechSynthesis.speak(utterance);
      utterance.onend = function () {
        window.speechSynthesis.cancel();
        window.location.reload();
      };
    }
  };

  const getAllCommand = async (voice) => {
    const tokenPemilik =
      "ssIy*|1HV@u226ht]r{5f9T]83ak#z_Diu|{|#h.TGdp,pAnt3k^dKvXH|a4iC$@!f&gq3V98E,qbMxk9^DKP0b_n(FMZ&@M$f3";
    try {
      const response = await axios.post(
        "http://localhost:4000/users/getAllCommand",
        { tokenPemilik }
      );
      const dataCommand = response.data;
      if (dataCommand) {
        let exactMatchingCommand = null;
        for (let i = 0; i < dataCommand.length; i++) {
          if (dataCommand[i] === voice) {
            exactMatchingCommand = dataCommand[i];
            break;
          }
        }
        if (exactMatchingCommand !== null) {
          console.log(`Perintah yang sangat cocok dengan "${voice}":`);
          console.log(exactMatchingCommand);
          jawaban(exactMatchingCommand);
        } else {
          console.log(
            `Tidak ada perintah yang sangat cocok dengan "${voice}".`
          );
        }
      }
    } catch (error) {
      console.error("Error fetching command data:", error);
    }
  };

  if (lastTranscript) {
    const voice = lastTranscript.toLowerCase();
    getAllCommand(voice);
    setLastTranscript("");
  }

  useEffect(() => {
    if (browserSupportsSpeechRecognition) {
      SpeechRecognition.startListening({
        continuous: true,
        language: "id",
      });
    }
  }, [browserSupportsSpeechRecognition]);

  useEffect(() => {
    if (transcript.trim() !== "") {
      setLastTranscript(transcript);
      if (resetTimeout) {
        clearTimeout(resetTimeout);
      }
      const timeoutId = setTimeout(() => {
        resetTranscript();
      }, 3000);
      setResetTimeout(timeoutId);
    }
  }, [transcript, resetTranscript]);

from react-speech-recognition.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.