DOM
SWT
HTML
Integration
Logging
Chromium features
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:
- 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.
- 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.
- In the top bar, create a new project for your app or select an existing one.
- Go to APIs & Services > Library from the hamburger menu.
- 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.
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
:
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"
}
}
- Go to APIs & Services > Credentials from the hamburger menu.
- 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.
- In the pop-up window, you’ll see a Client ID and a Client secret string. They should be passed to the
googleDefaultClientId
andgoogleDefaultClientSecret
methods respectively. - 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.
Granting permissions
To allow Chromium recognize your voice, you need to grant access to record audio:
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.