SpeechRecognition: available() static method

The available() static method of the Web Speech API checks whether specified languages are available for speech recognition.

To install a language pack for speech recognition locally, you need to use the SpeechRecognition.install() method.

Access to the available() method is controlled by the on-device-speech-recognition Permissions-Policy directive. Specifically, where a defined policy blocks usage, any attempts to call the method will fail.

Syntax

js
available(options)

Parameters

options

An object specifying options for the availability check. Possible properties include:

langs

An array of one or more strings containing BCP 47 language tags, each representing a language that you want to check availability of for speech recognition. Passing an empty langs array will not throw an error, but the return value will always resolve to unavailable.

processLocally Optional

A boolean that specifies whether you are checking availability of the specified languages for on-device speech recognition (true) or availability of the specified languages for on-device or remote speech recognition (false). Defaults to false.

Note: It is not possible to use available() to determine whether a remote service is guaranteed to support the specified languages. A value of false means that either an on-device or remote speech recognition service supports the specified languages.

Return value

A Promise that resolves with an emumerated value indicating the availability of the specified languages for speech recognition.

Possible values are:

available

Indicates that support for all the specified languages is available.

  • If processLocally is set to true, available means that speech recognition is available for those languages on-device (the required language packs have been downloaded and installed on the user's computer).
  • If processLocally is set to false, available means that speech recognition is available for those languages either on-device or remotely.
downloadable

Indicates that support for the specified languages is available on-device, but the relevant language packs have not yet been downloaded.

downloading

Indicates that support for the specified languages is available on-device, and the relevant language packs are in the process of being downloaded.

unavailable

Indicates that support for at least one of the specified languages is not available.

  • If processLocally is set to true, unavailable means that on-device speech recognition is not available for at least one of the specified languages.
  • If processLocally is set to false, unavailable means that speech recognition is not available for at least one of the specified languages either on-device or remotely.

Note: The downloadable and downloading values are primarily relevant when processLocally is set to true. If processLocally is set to false and the user agent prefers remote processing, available() will always return available or unavailable.

Exceptions

InvalidStateError DOMException

The current document is not fully active.

SyntaxError DOMException

One or more of the strings specified in langs is not a valid BCP 47 language tag.

Examples

Checking on-device availability and installing language packs

For on-device speech recognition to occur, the browser needs to have a language pack installed for the language you are trying to recognize. If you run the start() method after specifying processLocally = true and you haven't got the correct language pack installed, it will fail with a language-not-supported error.

To get the correct language pack installed, there are two steps to follow.

  1. You need to check whether the language pack is available on the user's computer using the available() method.
  2. You need to install the language pack if it isn't available using the SpeechRecognition.install() method.

Both of the above steps are handled using the following code snippet:

js
startBtn.addEventListener("click", () => {
  // check availability of target language
  SpeechRecognition.available({ langs: ["en-US"], processLocally: true }).then(
    (result) => {
      if (result === "unavailable") {
        diagnostic.textContent = `en-US not available to download at this time. Sorry!`;
      } else if (result === "available") {
        recognition.start();
        console.log("Ready to receive a color command.");
      } else {
        diagnostic.textContent = `en-US language pack downloading`;
        SpeechRecognition.install({
          langs: ["en-US"],
          processLocally: true,
        }).then((result) => {
          if (result) {
            diagnostic.textContent = `en-US language pack downloaded. Try again.`;
          } else {
            diagnostic.textContent = `en-US language pack failed to download. Try again later.`;
          }
        });
      }
    },
  );
});

We run the available() method, specifying one language (langs: ["en-US"]) to check availability for, and processLocally: true. We test for three different possibilities of the return value:

  • If the resulting value is unavailable, it means that the language is not available, and a suitable language pack is not available to download, so we print an appropriate message to the output.
  • If the resulting value is available, it means that the language pack is available locally, so recognition can begin. In this case, we run start() and log a message to the console when the app is ready to receive speech.
  • If the value is something else (downloadable or downloading), we print a diagnostic message to inform the user that a language code download is commencing, then run the install() method to handle the download.

The install() method works in a similar way to the available() method, except that its options object only takes the langs array. When run, it starts downloading the en-US language pack and returns a Promise that resolves with a boolean indicating whether the specified language packs were downloaded and installed successfully (true) or not (false).

This code is excerpted from our on-device speech color changer (run the demo live). See Using the Web Speech API for a full explanation.

Specifications

Specification
Web Speech API
# dom-speechrecognition-available

Browser compatibility

See also