Raddy Website Design & Development Tutorials

annyang! Speech Recognition with JavaScript Tutorial

By Raddy in JavaScript ·

Annyang is a tiny javascript library that lets your visitors control your site with voice commands. annyang supports multiple languages, has no dependencies, weighs just 2kb and is free to use.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Annyang!</title>
</head>
<body>

    <script src="//cdnjs.cloudflare.com/ajax/libs/annyang/2.6.0/annyang.min.js"></script>
    <script>

        var messages = ['🔊 Hey', '🔊 Hi, there!', '🔊 Hi!', '🔊 Hello'];

        if(annyang){
            console.log("We have annyang!");

            var commands = {
                'Hello' : hello,
                'What is your name' : myname
            }

            function hello(){
                var randomIndex = Math.round(Math.random() * messages.length);
                console.log(`%c ${messages[randomIndex]}`,'color: green; font-weight:bold;');
            }

            function myname(){
                console.log("My name is Billy!");
            }

            // Add Commands
            annyang.addCommands(commands);

            // Start listening
            annyang.start();

        }

    </script>
</body>
</html>
  1. Adebayo says:

    I’ve tried to run bellow annoyang code on my system but not working. it displays noting.

    if (annyang) {
    Alert (‘annyang is working’);
    }

    1. Raddy says:

      That’s strange. Try it in any Chromium browser and see if that works.

    2. jordan says:

      prueba lo con el console.log() a mi si me funciona correctamente

  2. Milind malani says:

    Hi i’m trying to make a voice driven website..i want to add commands like ‘ move to the next page’ or ‘previous page’..how do i do that?

    1. Raddy says:

      I’ve already done something similar to what you are asking. There is a Video Tutorial, a Written tutorial and a Demo. Here is the tutorial:
      https://raddy.dev/blog/create-a-custom-smart-assistant-device-using-web-technologies/

      The only difference is that I use scroll instead of a page change.
      Maybe you can do “location.href = ‘/mypage.html’;” instead and that should work.

      I hope this helps 🙂

  3. milind malani says:

    and also can i contact you to make this work…its very urgent..

  4. milind malani says:

    if(annyang){
    console.log(“we have annyang”);

    var commands = {

    ‘show home’ : home,

    }

    function home(){
    console.log(“Home”);
    “location.href = ‘https://valkeshwarmahadev.in/gallery.html’;”

    }

    i tried this but i dint got the output bro

  5. milind malani says:

    if(annyang){
    console.log(“we have annyang”);

    var commands = {

    ‘show home’ : home,

    }

    function home(){
    console.log(“Home”);
    “location.href = ‘https://valkeshwarmahadev.in/gallery.html’;”

    }

    i tried this but i dint got the output bro..i also added js library in the src tag

    1. Raddy says:

      No double-quotes. Just location.href….

      1. milind malani says:

        still not getting the result bro..if you can check it on the website that would be really helpful.

        1. Raddy says:

          Try this instead: window.location.href = “https://valkeshwarmahadev.in/gallery.html”;
          I just tested it on mine and seems to work. Let me know how it goes

          1. milind malani says:

            when i tried this the system is not accessing the microphone..what shall i do now bro?

          2. milind malani says:

            can u check the website and inspect it?

  6. milind malani says:

    tried many variations bro but couldnt get the result..can u visit my website once and inspect it..

  7. milind malani says:

    hey did u check it?

    1. Raddy says:

      I did and I can’t seem to find anything Annyang on your website.
      I checked scripts.min.js, main.min.js and custom.js…

      1. milind malani says:

        bro you can check the source code on the home page and their you will find the annyang code written..the website is valkeshwarmahadev.in..cant place the link as it denies my request over here..pls check it

        1. Raddy says:

          You have to close your home function and double-check the double quotes and the single quotes as sometimes when you copy them from the comments here they break.

          if (annyang) {
          console.log(“we have annyang”);
          var commands = {
          ‘show home’: home,
          }

          function home() {
          console.log(“Home”);
          window.location.href = “https://valkeshwarmahadev.in/gallery.html”;
          }

          // Add Commands
          annyang.addCommands(commands);

          // Start listening
          annyang.start();
          }

          I hope this helps and also look at their official documentation, which might help you out. I hope that you fix it brother

          1. milind malani says:

            i tried this bro but the system is not acessing microphone only..i read the official document also but nothing is working..

          2. milindmalani says:

            tried everything brother but can’t get the output..i’m working as a trainee in a well known organisation and they have assigned me a task and i really wanna do this task.

  8. milind malani says:

    i’m working as a trainee bro in a well known organisation and they have assigned me a task..i need to do this any how bro..

  9. milind malani says:

    can we connect personally to solve this pls?

    1. Raddy says:

      I am happy to guide you to solve your problem, but I can’t do your assignment. The last thing you can try is to debug the problem. You can use Annyang Debug to see what the problem is.
      annyang.debug();
      Inspect your page, go to Console, wait a few seconds and see what it says.

      As you are a trainee, your organisation should be helping you. Ask them for help, you are there to learn…

  10. milind malani says:

    ofcourse bro i’ll do this by myself only and the thing is all the junior folks in the company haven’t been joined yet and they are working from home due to covid so i cant ask anybody over here..its just u can help me doing in this..i debugged the code and found nothing still the system is not accessing the microphone only..pls suggest me something its really imp for me or i’ll be jobless

  11. milind malani says:

    as it is the first task given to me by the HOD , so i need to complete anyhow i’m working on ds and algo to build my logics but that will take something unless i need to survive..

  12. milind malani says:

    thank u so much , it is working brother…now if i want to add comands like scroll up and down what shall i do?

  13. milind malani says:

    and bro i tried to add another command named ‘contact page’ and used the same logic as above but it wasnt working and even the previous one was not working what could be the reason?

  14. milind malani says:

    thank u so much , it is working brother…now if i want to add comands like scroll up and down what shall i do?and bro i tried to add another command named ‘contact page’ and used the same logic as above but it wasnt working and even the previous one was not working what could be the reason?

  15. milind malani says:

    bro did u check the previous comments?

    1. Raddy says:

      Did you watch or read the article? I literally explain everything in there step by step and I have many examples.
      You can read more about functions here: https://www.w3schools.com/js/js_functions.asp

      1. milind malani says:

        yes i did bro and everything is working but i want to add ‘scroll down’ and ‘scroll up’ commands within the page..i tried location.hash but it isnt working..

      2. milind malani says:

        and supposedly bro in this website only which i’m working on i need to add a command like write a message in contact page how would i execute it?

  16. milind malani says:

    ik i’m kinda being annoying to your brother but its really important..

    1. Raddy says:

      Somewhere down your page (you need to have some scroll) you can place HTML anchors:

      To invoke it you do location.hash and then the name of the anchor scrollToGallery.
      location.hash = “scrollToGallery”;

      Full function example:
      function gallery() {
      console.log(“Gallery”);
      location.hash = “scrollToGallery”;
      }

      To test your anchor you can also put #anchorToGallery after your URL.
      mywebsite.com#scrollToGallery

      That should then scroll to your placed anchor. If you don’t have a scroll on your page it obviously has nowhere to movie/scroll.

      I have so many examples here and I don’t know why you are not using them…
      https://raddy.dev/blog/create-a-custom-smart-assistant-device-using-web-technologies/

  17. milind malani says:

    done bro can see the results , but i want a command as if i say ‘write a message in contact page’ so it can execute that..for that what shall i do bro?

    1. milind malani says:

      and bro thank you for helping this much , means a lot but pls i’ll ask u questions untill i get the results

  18. milind malani says:

    i got the output in scroll up an down but bro i need to complete this one where i command ‘ write a message in contact page’ for that do what shall i need to do

    1. Raddy says:

      I think that Annyang is only for voice commands. I am not sure if you can use it to write messages. For speech to text, you could use Speech to Text Cloud API. I think that Google and Microsoft have one. Both are not free, but I would assume that they are going to be really good.

  19. milind malani says:

    right..but bro if i wanna make a voice driven website without annyang is it possible? like the logic of the code is same but i want the code to access my microphone and run the commands..

    1. Raddy says:

      You can use Speech Recognition API, but the browser support is not great at the moment. Do some research and see what else is out there.

      You don’t have to repost your questions multiple times. Sometimes the comments just go to spam and I approve them whenever I can.

      1. milind malani says:

        i have tried speech recognition API but it isnt working in browser..and bro instead of making a voice driven website can we code for a voice driven browser? ik there are some already readymate software’s

        1. Raddy says:

          There probably is something made for accessibility out there. Technically speaking our operating systems have a lot of accessibility functionalities that can be used to navigate thru websites. As long as you make your website accessible by following the a11y standards you should be good.

          What are you actually trying to achieve?

          1. milind malani says:

            Actually the AVP has given me a task of including voice driven functions on the admin pannel so they dont have to click anything they can just control the pannel from voice for an eg: if somebody wants to see notifications they can just call out notifications and all the noti comes to the screen..so ya voice control idea..

  20. milind malani says:

    Do u have any idea on how do i achieve that?

    1. Raddy says:

      Annayang or the Web Speech API. Speak with your team and see what they say.

Comments are closed.