Skip to content

Commit

Permalink
Merge pull request #104 from TalAter/niki4810-master
Browse files Browse the repository at this point in the history
Adding pause and resume methods
  • Loading branch information
TalAter committed Mar 12, 2015
2 parents 8a8bb25 + 93c3e22 commit 219d710
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 14 deletions.
44 changes: 42 additions & 2 deletions annyang.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
var lastStartedAt = 0;
var debugState = false;
var debugStyle = 'font-weight: bold; color: #00f;';
var pauseListening = 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 @@ -163,6 +164,13 @@
};

recognition.onresult = function(event) {
if(pauseListening) {
if (debugState) {
root.console.log('Speech heard, but annyang is paused');
}
return false;
}

invokeCallbacks(callbacks.result);
var results = event.results[event.resultIndex];
var commandText;
Expand Down Expand Up @@ -223,6 +231,7 @@
* @method start
*/
start: function(options) {
pauseListening = false;
initIfNeeded();
options = options || {};
if (options.autoRestart !== undefined) {
Expand All @@ -235,11 +244,20 @@
}

lastStartedAt = new Date().getTime();
recognition.start();
try {
recognition.start();
} catch(e) {
if (debugState) {
root.console.log(e.message);
}
}
},

/**
* Stop listening.
* Stop listening, and turn off mic.
*
* Alternatively, to only temporarily pause annyang responding to commands without stopping the SpeechRecognition engine or closing the mic, use pause() instead.
* @see [pause()](#pause)
*
* @method abort
*/
Expand All @@ -250,6 +268,28 @@
}
},

/**
* Pause listening. annyang will stop responding to commands (until the resume or start methods are called), without turning off the browser's SpeechRecognition engine or the mic.
*
* Alternatively, to stop the SpeechRecognition engine and close the mic, use abort() instead.
* @see [abort()](#abort)
*
* @method pause
*/
pause: function() {
pauseListening = true;
},

/**
* Resumes listening and restores command callback execution when a result matches.
* If SpeechRecognition was aborted (stopped), start it.
*
* @method resume
*/
resume: function() {
root.annyang.start();
},

/**
* Turn on output of debug messages to the console. Ugly, but super-handy!
*
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.

2 changes: 1 addition & 1 deletion demo/css/main.min.css

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

Loading

0 comments on commit 219d710

Please sign in to comment.