HTML5 Speech Input with Voice Recognition

H
Published in: Javascript

HTML5 support speech input x-webkit-speech and this has been implemented in the newer versions of Google Chrome.

Dictation is an online speech recognition app that uses Google’s Chrome speech engine to help you convert your spoken words into text.

The JavaScript code that powers the Dictation app is here:

// Written by Amit Agarwal on 10/08/2012
// See https://dictation.io for live demo

$(document).ready(function () {
  // Check if the user's web browser supports HTML5 Speech Input API
  if (document.createElement("input").webkitSpeech == undefined) {
    $(".answer").append("We are sorry but Dictation requires Google Chrome.");
  } else {
    // Get the default locale of the user's browser (e.g. en-US, or de)
    var language = window.navigator.userLanguage || window.navigator.language;
    $("#speech").attr("lang", language).focus();

    // Make the text region editable to easily fix transcription errors
    $(".answer").click(function () {
      $(".answer").attr("contentEditable", "true");
    });
  }

  // This is called when Chrome successfully transcribes the spoken word
  $("#speech").bind("webkitspeechchange", function (e) {
    var val = $(this).val();

    // Did the user say Delete? Then clear the canvas.
    if (val == "delete everything") {
      $(".answer").text("");
      return;
    }

    // For "new line" commands, add double line breaks.
    if (val == "new line") val = "<br /><br />";
    else {
      // Capitalize the first letter of the sentence.
      val = val.substr(0, 1).toUpperCase() + val.substr(1);

      // If the last letter is a alphanumeric character, add a period (full stop)
      if (val.match(/[a-zA-Z]$/)) val = val + ".";
    }

    // Append the transcribed text but set the focus to the hidden speech input.
    // This enables keyboard shortcut Ctrl+Shift+Period (.) for speech mode.
    $(".answer")
      .append(val + " ")
      .fadeIn();
    $(this).val("").focus();
  });
});
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Javascript

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube