chrome.experimental.speechInput

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.

Manifest

You must declare the "experimental" permission in the extension manifest to use the speech input API. For example:

{
  "name": "My extension",
  ...
  "permissions": [
    "experimental"
  ],
  ...
}

How to start speech recognition

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.runtime.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.runtime.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.

How to get speech recognition results

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 experimental.speechInput.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.

How to stop recording

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.runtime.lastError will be set.

Other features

Examples

The following example illustrates how to show a JavaScript alert with the most likely recognition result.

function checkStart() {
  if (chrome.runtime.lastError) {
    alert("Couldn't start speech input: " + chrome.runtime.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(recognitionSucceeded);
chrome.experimental.speechInput.start({ "language": "en" }, checkStart);

chrome.experimental.speechInput reference

Types

SpeechInputStartOptions

Object describing the options used for speech recognition.

Properties of SpeechInputStartOptions

language ( optional string )
BCP-47 language code of the language to recognize. When set to 'auto' or not defined defaults to user's most preferred content language. Will use 'en-US' if not supported or invalid.
grammar ( optional string )
Name of the grammar to use. Defaults to 'builtin:dictation'.
filterProfanities ( optional boolean )
Enable or disable profanity filtering. Will use the default Chrome filtering settings if not set.

SpeechInputError

Object describing a speech input error.

Properties of SpeechInputError

code ( enumerated string ["noSpeechHeard", "noResults", "captureError", "networkError"] )
Code identifying the error case.

SpeechInputResultHypothesis

Object describing a speech recognition hypothesis.

Properties of SpeechInputResultHypothesis

utterance ( string )
Text of this hypothesis.
confidence ( double )
Confidence of the hypothesis. Rated from 0 (lowest) to 1 (highest).

SpeechInputResultEvent

Object containing the recognition results.

Properties of SpeechInputResultEvent

hypotheses ( optional array of SpeechInputResultHypothesis )
Array of zero or more objects describing the stable candidate hypotheses sorted by decreasing likelihood.

Methods

start

chrome.experimental.speechInput.start(SpeechInputStartOptions options, function callback)

Request to start recording as a new speech recognition session.

Parameters

options ( optional SpeechInputStartOptions )
Options used for this speech recognition session.
callback ( optional function )
Called when the speech recognition session has successfully started recording.

Callback

If you specify the callback parameter, it should specify a function that looks like this:

function() {...};

stop

chrome.experimental.speechInput.stop(function callback)

Request to stop an ongoing speech recognition session.

Parameters

callback ( optional function )
Called when the audio recording has stopped and any pending recognition results have been completed.

Callback

If you specify the callback parameter, it should specify a function that looks like this:

function() {...};

isRecording

chrome.experimental.speechInput.isRecording(function callback)

Determine if audio recording is currently in process in Chrome, not limited to this API.

Parameters

callback ( optional function )

Callback

If you specify the callback parameter, it should specify a function that looks like this:

function(boolean result) {...};
result ( boolean )
Flag set to true if recording is in process in Chrome, false otherwise.

Events

onError

chrome.experimental.speechInput.onError.addListener(function(SpeechInputError error) {...});

Called in case of an error in speech recognition.

Listener Parameters

error ( SpeechInputError )
Error being reported.

onSoundStart

chrome.experimental.speechInput.onSoundStart.addListener(function() {...});

Called when the system starts detecting sound in the input data.

onSoundEnd

chrome.experimental.speechInput.onSoundEnd.addListener(function() {...});

Called when the system detects enough silence to consider the ongoing speech has ended.

onResult

chrome.experimental.speechInput.onResult.addListener(function(SpeechInputResultEvent event) {...});

Called when speech recognition results are available.

Listener Parameters

event ( SpeechInputResultEvent )
Object containing the speech recognition results.

Sample Extensions that use chrome.experimental.speechInput

  • Speech Recognizer – Recognizes your speech and tells you the most likely result.