Voice Service Integration
Objective
Integrate voice commands in your Android applications using the voice service of the Almer Arc.
Interaction between your App and Service
The application and the service will communicate using broadcasts. In the following examples you will find the functionalities of the voice service alongside code samples to fully acknowledge how to integrate the voice commands in your app.
1. Registering voice commands
Voice commands can be registered by sending a list to the Voice Service using an intent:
- kotlin
- java
companion object {
// Intention to add new voice commands
private const val ACTION_OVERRIDE = "com.almer.voiceassistant.action.OVERRIDE_COMMANDS"
// List of commands to be added
private const val EXTRA_COMMANDS = "com.almer.voiceassistant.extra.COMMANDS"
// Package name of the app that sends the voice commands
private const val EXTRA_SOURCE = "com.almer.voiceassistant.extra.SOURCE_PACKAGE"
}
private fun sendCommands() {
val voiceCommands = arrayOf(
"Turn Camera on",
"Turn Camera off"
)
val intent = Intent(ACTION_OVERRIDE)
intent.putExtra(EXTRA_SOURCE, packageName)
intent.putExtra(EXTRA_COMMANDS, voiceCommands)
sendBroadcast(intent)
}
// Intention to add new voice commands
private final String ACTION_OVERRIDE = "com.almer.voiceassistant.action.OVERRIDE_COMMANDS";
// List of commands to be added
private final String EXTRA_COMMANDS = "com.almer.voiceassistant.extra.COMMANDS";
// Package name of the app that sends the voice commands
private final String EXTRA_SOURCE = "com.almer.voiceassistant.extra.SOURCE_PACKAGE";
private void sendCommands() {
String[] voiceCommands = { "Turn Camera on", "Turn Camera off" };
Intent intent = new Intent(ACTION_OVERRIDE);
intent.putExtra(EXTRA_SOURCE, getPackageName());
intent.putExtra(EXTRA_COMMANDS, voiceCommands);
sendBroadcast(intent);
}
2. Reacting to voice command triggers
- Applications can listen for when the speech recognizer has detected a spoken voice command (e.g "Enter text <...>") by using a BroadcastReceiver:
- kotlin
- java
companion object {
// Action for the broadcast receiver intent filter: voice service detected a command
private const val ACTION_SPEECH_EVENT = "com.almer.voiceassistant.action.SPEECH_EVENT"
// The detected voice command
private const val EXTRA_COMMAND = "com.almer.voiceassistant.extra.COMMAND"
}
private val speechBroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == ACTION_SPEECH_EVENT) {
val command = intent.getStringExtra(EXTRA_COMMAND)
}
}
}
// Action for the broadcast receiver intent filter: voice service detected a command
private final String ACTION_SPEECH_EVENT = "com.almer.voiceassistant.action.SPEECH_EVENT";
// The detected voice command
private final String EXTRA_COMMAND = "com.almer.voiceassistant.extra.COMMAND";
private final BroadcastReceiver speechBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null && intent.getAction().equals(ACTION_SPEECH_EVENT)) {
String command = intent.getStringExtra(EXTRA_COMMAND);
}
}
};
3. Restore normal services
When your application has finished with the speech recogniser it should send a message to restore default behaviour, using the RESTORE_COMMANDS intent.
- kotlin
- java
companion object {
// Resets the voice service to default commands
private const val ACTION_RESTORE = "com.almer.voiceassistant.action.RESTORE_COMMANDS"
}
private fun restoreVoiceCommands() {
val intent = Intent(ACTION_RESTORE)
sendBroadcast(intent)
}
// Resets the voice service to default commands
private final String ACTION_RESTORE = "com.almer.voiceassistant.action.RESTORE_COMMANDS";
private void restoreVoiceCommands() {
Intent intent = new Intent(ACTION_RESTORE);
sendBroadcast(intent);
}
4. API
Intent | Description |
---|---|
com.almer.voiceassistant.action.OVERRIDE_COMMANDS | Used to take control of the speech recogniser |
com.almer.voiceassistant.action.RESTORE_COMMANDS | Used to reset the speech recogniser to its default behavior |
com.almer.voiceassistant.action.SPEECH_EVENT | Sent from the Voice Service to identify spoken voice commands |
Extra | Data Type | Description |
---|---|---|
com.almer.voiceassistant.extra.COMMANDS | String[] | Used to pass in a list of voice commands to the speech recogniser |
com.almer.voiceassistant.extra.SOURCE_PACKAGE | String | Used to pass the application's package name to the speech recogniser |
com.almer.voiceassistant.extra.COMMAND | String | Passed from the speech recogniser when a voice command is spoken. Identifies the command spoken by the user |