Search code examples
webspeech-recognitionmicrophone

Web speech recognition automatically disconnects


So I have a project where I need to make a Web based Virtual Assistant. The problem is that the Web Speech Recognition Api automatically disconnects the microphone if it does not hear something in like 5 seconds (so I need to turn it back on). My question is how can I keep the mic active so that when I say the wake word it uses the next recognition result as a command. *Extra explication: -The mic should be always listening so that when I say the wake word it hears it. -If I don't speak for about 5 seconds the mic disconnects so I need to turn it back on manually (how to get rid of this auto-disconnect?) Thanks!


Solution

  • Depending on your browser and if any tabs are trying to access your mic, setting your SpeechRecognition object to continuous should work.

    Use the code below for reference. The window.SpeechRecognition is just setting up the speech recognition.

    window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
    
    const recognition = new SpeechRecognition();
    recognition.continuous = true;
    

    You can also add an event listener for when your recognition disconnects and start back up the recognition of the web speech api.

    recognition.addEventListener('end', () => {
        recognition.start();
      });
    

    This article, from our code world, on the speech api also goes over storing the interim words spoken while it's continuous.

    This worked for me on the Chrome browser.

    If setting the recognition to continuous does not work, you may have to troubleshoot another service on your computer taking the microphone from your browser tab.