List icon Contents

Voice recognition

This tutorial demonstrates how to enable voice recognition in JxBrowser.

Enabling Speech API

Voice recognition is a Chromium proprietary feature that requires a Google API key to run properly. Before acquiring that key, you need to first enable private Speech API:

  1. This API is visible only to people subscribed to the chromium-dev Google Group. When joining, make sure the Link to my Google account profile check is set.
  2. Go to the Google Cloud console. Make sure you are logged in with the Google account associated with the email address that you used to subscribe to the “chromium-dev” group.
  3. In the top bar, create a new project for your app or select an existing one.
  4. Go to APIs & Services > Library from the hamburger menu.
  5. Find and enable “Speech API”, NOT “Cloud Speech-to-Text API”. If there’s no such API found, return to the Library page and choose Private in the Visibility section on the left. There will be all private APIs visible to you.

Speech API

Appearance of private APIs after subscribing to the group may take some time (up to 15 minutes).

Acquiring credentials

Now you are ready to acquire credentials that should be passed to EngineOptions:

Java
Kotlin
var options = EngineOptions.newBuilder(HARDWARE_ACCELERATED)
        .googleApiKey("key")
        .googleDefaultClientId("client-id")
        .googleDefaultClientSecret("secret")
        .build();
var engine = Engine.newInstance(options);
val engine = Engine(HARDWARE_ACCELERATED) {
      google {
         apiKey = "key"
         defaultClientId = "client-id"
         defaultClientSecret = "secret"
      }
}
  1. Go to APIs & Services > Credentials from the hamburger menu.
  2. Click the CREATE CREDENTIALS button, and select the OAuth client ID item in the drop-down list. In the Application type section, check the Desktop app and give it a name in the Name text box, then click Create.
  3. In the pop-up window, you’ll see a Client ID and a Client secret string. They should be passed to the googleDefaultClientId and googleDefaultClientSecret methods respectively.
  4. Click the CREATE CREDENTIALS button once again on the same page. Choose the API key item. A pop-over should show up giving you the API key string. It should be passed to the googleApiKey method.

Please, take into account that enabling API and acquiring credentials is not an immediate action. In Google Cloud, it takes some time to apply those settings so you could use them. The platform even reminds you this when creating the credentials.

Create Client Credentials

Granting permissions

To allow Chromium recognize your voice, you need to grant access to record audio:

Java
Kotlin
engine.permissions().set(RequestPermissionCallback.class, 
                         (params, tell) -> {
                            if (params.permissionType() == AUDIO_CAPTURE) {
                                tell.grant();
                            } else {
                                tell.deny();
                            }
                          });
engine.permissions().set(RequestPermissionCallback::class.java) { params, tell ->
   if (params.permissionType() === AUDIO_CAPTURE) {
      tell.grant()
   } else {
      tell.deny()
   }
}

Please, check out the full code snippet.