The chrome.experimental.speechInput
module provides
one-shot speech recognition to Chrome extensions.
This module is still experimental. For information on how to use experimental
APIs, see the chrome.experimental.* APIs page.
You must declare the "experimental" permission in the extension manifest to use the speech input API. For example:
{ "name": "My extension", ... "permissions": [ "experimental" ], ... }
To start recognizing speech an extension must call the start()
method. If provided, your callback will be called once recording has
successfully started. In case of error chrome.extension.lastError
will be set.
This API provides exclusive access to the default recording device to the
first extension requesting it. Consequently, any calls to start()
when the device is being used by another extension or web page will fail and set
chrome.extension.lastError
. The message requestDenied
will be set if another extension in the same profile is making use of the API.
Otherwise noRecordingDeviceFound
, recordingDeviceInUse
or unableToStart
will be set depending on the situation.
To check whether recording is currently active, call the
isRecording()
method. Please note that it only checks for audio
recording within Chrome.
Listen to the onResult
event to receive speech recognition
results.
var callback = function(result) { ... }; chrome.experimental.speechInput.onResult.addListener(callback);
The result
object contains an array of
SpeechInputResultHypothesis
sorted by decreasing likelihood.
Recording automatically stops when results are received. It is safe to call
start()
again from the results callback.
To handle errors during speech recognition listen for the
onError
event.
var callback = function(error) { ... }; chrome.experimental.speechInput.onError.addListener(callback);Recording will automatically stop in case of error. It is safe to call
start()
again from the error callback.
To stop speech recognition call the stop()
method. If provided,
the callback function will be called once recording has successfully stopped.
In case of error chrome.extension.lastError
will be set.
onSoundStart
- Event generated when start of sound is detected
(from previously being silent).
onSoundEnd
- Event generated when end of sound is detected (a
continued period of silence).
The following example illustrates how to show a JavaScript alert with the most likely recognition result.
function checkStart() { if (chrome.extension.lastError) { alert("Couldn't start speech input: " + chrome.extension.lastError.message); } } function recognitionFailed(error) { alert("Speech input failed: " + error.code); } function recognitionSucceeded(result) { alert("Recognized '" + result.hypotheses[0].utterance + "' with confidence " + result.hypotheses[0].confidence); } chrome.experimental.speechInput.onError.addListener(recognitionFailed); chrome.experimental.speechInput.onResult.addListener(recognitionSucceded); chrome.experimental.speechInput.start({ "language": "en" }, checkStart);