Home Technology

How to Add Speech Recognition to your Website Or Blog

How to Add Speech Recognition to your Website Or Blog
How to Add Speech Recognition to your Website Or Blog

Discover the best way to enable speech recognition in your website using the HTML5 web speech API. Your website website will be able to fill text areas and input form fields using their voice.

Get started by opening  Google website on your desktop computer and you’ll find a little microphone icon embedded inside the search box. Click the icon, make a speech or say anything you like, your voice is quickly transcribed into words. Unlike earlier speech recognition products, you no longer have to train the browser to understand your speech and, for those who don’t know touch typing, speech is often a faster mode of input than the keyboard.

READ ALSO:How To Easily Create a Paga Account Online

Does it sound  like magic? However, know that you can also include the same speech recognition capabilities to your own website with a few lines of code. Visitors can search your website, or even fill forms, using just their voice. Both Google Chrome and Firefox browsers support the speech recognition API.
Before we dive into the actual implementation, let’s play with a working demo. If you are viewing this page inside Google Chrome (desktop or mobile), click the voice icon inside the search box and say a search query. You may have allow the browser to access your microphone. When you are done speaking, the search results page will open automatically.
How to Add Speech Recognition to your Website Or Blog

Add Voice Recognition to your Website

The HTML5 Web Speech API has been made public and activated few years now but it takes slightly more work now to include it in your website.

Read Also:How To Easily Connect Your Laptop/PC To Your Television Screen

Earlier, you could add the attribute x-webkit-speech to any form input field and it would become voice capable. The x-webkit-speech attribute has nonetheless been deprecated and you are now required to use the JavaScript API to include speech recognition. Here’s the updated code:
<!-- CSS Styles -->
<style>
  .speech {border: 1px solid #DDD; width: 300px; padding: 0; margin: 0}
  .speech input {border: 0; width: 240px; display: inline-block; height: 30px;}
  .speech img {float: right; width: 40px }
</style>

<!-- Search Form -->
<form id="labnol" method="get" action="https://www.google.com/search">
  <div class="speech">
    <input type="text" name="q" id="transcript" placeholder="Speak" />
    <img onclick="startDictation()" src="//i.imgur.com/cHidSVu.gif" />
  </div>
</form>

<!-- HTML5 Speech Recognition API -->
<script>
  function startDictation() {

    if (window.hasOwnProperty('webkitSpeechRecognition')) {

      var recognition = new webkitSpeechRecognition();

      recognition.continuous = false;
      recognition.interimResults = false;

      recognition.lang = "en-US";
      recognition.start();

      recognition.onresult = function(e) {
        document.getElementById('transcript').value
                                 = e.results[0][0].transcript;
        recognition.stop();
        document.getElementById('labnol').submit();
      };

      recognition.onerror = function(e) {
        recognition.stop();
      }

    }
  }
</script>
Currently the  CSS platform to place the microphone image inside the input box is available, the form code containing the input button and the JavaScript that does all the heavy work.
When the user click the mic image inside the search box, the JavaScript checks if the user’s browser supports speech recognition. If so, it waits for the transcribed text to arrive from Google servers and then submits the form.
The Dictation App also uses the speech recognition API though it writes the transcribed text to textarea field instead of an input box.
N/B
  1. You can decide to change the value of the recognition.lang feature from ‘en-US’ to another language (like hi-In for Hindi or fr-FR for Français). See the complete list of supported languages.
  2. If the HTML form / search box is embedded inside an HTTPS website, the browser will not repeatedly ask for permission to use the microphone.