-1

I am currently looking into implementing a feature where users of my application can navigate to a different part of the site by entering text or using speech. The following code is used in my project using google's speech recognition API, and it allows users to enter text to perform a google search or use spoken-word to achieve the same result. I was wondering if anyone could give me some pointers in the right direction where I could use this code to search my application when launched that is if a user spoke/typed forum then it would navigate to the index forum view in localhost. Thanks in advance, the code is below.

Code for google search using voice/text

<form id="labnol" method="get" target="_blank" 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>

Script for speech dictation google search

<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>
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Plumbus
  • 31
  • 1
  • 1
  • 6

1 Answers1

0

Solved by implementing the following (seems easy but I had a brain fart)

HTML for application search

<form id="labnol1">
    <div class="speech">
        <input type="text" name="q2" id="transcript2" placeholder="Speak" />
        <img onclick="startDictationApplication()" src="//i.imgur.com/cHidSVu.gif" />
    </div>
</form>

Script

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Content/sweetalert.min.js"></script>

<script>
    function startDictationApplication() {

        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('transcript2').value
                                         = e.results[0][0].transcript;

                recognition.stop();
                if (document.getElementById('transcript2').value == "forum") {
                    swal({
                        type: "success",
                        title: "Loud and Clear!",
                        text: "Taking you to the forum!",
                        timer: 2000,
                        showConfirmButton: false
                    });
                    window.location.href = 'http://localhost:number/relevant view';
                } else {
                    swal({
                        type: "error",
                        title: "No such page!",
                        text: "Please refer to supported voice recogniton pages and try again!",
                        timer: 2000,
                        showConfirmButton: false
                    });
                }
            };

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

        }
    }
</script>
Plumbus
  • 31
  • 1
  • 1
  • 6