Deepak Jain
Deepak Jain

Reputation: 137

How to make speech recognition continous for a fix time period?

I want to make my speech recognition JavaScript to record continuously for a Fixed time period let's say 5 min.

my js has the following code

try {
  var SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
  var recognition = new SpeechRecognition();
}
catch(e) {
  // console.error(e);
  $('.no-browser-support').show();
  //$('.app').hide();
}

var noteTextarea = $('#note-textarea');
var instructions = $('#recording-instructions');
var notesList = $('ul#notes');

var noteContent = '';

// Get all notes from previous sessions and display them.
var notes = getAllNotes();
renderNotes(notes);


$( document ).ready(function() {
  // Handler for .ready() called.
   if (noteContent.length) {
      noteContent += ' ';
   }

   recognition.start();

});

Upvotes: 2

Views: 1947

Answers (1)

Muhammad Usman
Muhammad Usman

Reputation: 10148

The onend property of the SpeechRecognition interface represents an event handler that will run when the speech recognition service has disconnected (when the end event fires). So you can start SpeechRecognition again when this event occurs with start()

So, finally you should have something like

var recognition = new SpeechRecognition();
 ...

recognition.onend = function() {
  recognition.start();
}

To make it go for five minutes, you can use timer etc. like setInterval and add a check in onend callaback to check to start recognition again or not. Something like

var counter = 0;
var interval = setInterval(function(){
      counter++;
},1000)


recognition.onend = function() {
 if(counter <= 5 * 60)
  recognition.start();
 else
   clearInterval(interval)
}

And please not that recognition stops exactly after 5 minutes but for minimum 5 minutes

MDN

Upvotes: 2

Related Questions