Skip to content

Commit

Permalink
Added new method annyang.isListening()
Browse files Browse the repository at this point in the history
Returns true if speech recognition is currently on.
Returns false if speech recognition is off or annyang is paused.
  • Loading branch information
TalAter committed Dec 24, 2015
1 parent ee0e1c9 commit ce88ae3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
18 changes: 17 additions & 1 deletion annyang.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
var debugState = false;
var debugStyle = 'font-weight: bold; color: #00f;';
var pauseListening = false;
var isListening = false;

// The command matching code is a modified version of Backbone.Router by Jeremy Ashkenas, under the MIT license.
var optionalParam = /\s*\((.*?)\)\s*/g;
Expand Down Expand Up @@ -135,7 +136,10 @@
// Sets the language to the default 'en-US'. This can be changed with annyang.setLanguage()
recognition.lang = 'en-US';

recognition.onstart = function() { invokeCallbacks(callbacks.start); };
recognition.onstart = function() {
isListening = true;
invokeCallbacks(callbacks.start);
};

recognition.onerror = function(event) {
invokeCallbacks(callbacks.error);
Expand All @@ -158,6 +162,7 @@
};

recognition.onend = function() {
isListening = false;
invokeCallbacks(callbacks.end);
// annyang will auto restart if it is closed automatically and not by user action.
if (autoRestart) {
Expand Down Expand Up @@ -458,6 +463,17 @@
return;
}
callbacks[type].push({callback: cb, context: context || this});
},

/**
* Returns true if speech recognition is currently on.
* Returns false if speech recognition is off or annyang is paused.
*
* @return boolean true = SpeechRecognition is on and annyang is listening
* @method isListening
*/
isListening: function() {
return isListening && !pauseListening;
}
};

Expand Down
2 changes: 1 addition & 1 deletion annyang.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ annyang.addCallback('errorNetwork', notConnected, this);
* **Function** *callback* - The function to call when event is triggered
* **Object** *[context]* - Optional context for the callback function

## isListening()

Returns true if speech recognition is currently on.
Returns false if speech recognition is off or annyang is paused.

### Return:

* boolean true = SpeechRecognition is on and annyang is listening

# Good to Know

## Commands Object
Expand Down
2 changes: 1 addition & 1 deletion sites/facebook.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ce88ae3

Please sign in to comment.