No More "Hey Google"! Add your Wake Phrase to an Android app
Dilek Karasoy
Posted on February 24, 2023
You cannot change Google hotwords "Hey Google", "OK Google" but you can get your Android app have its own wake phrase with Porcupine Wake Word
Let's get started
Add the Porcupine Wake Word Library
Make sure you have a reference to Maven Central in your project’s build.gradle
file:
repositories {
mavenCentral()
}
Add the following reference to your app’s
build.gradle
file:
dependencies {
implementation 'ai.picovoice:porcupine-android:${LATEST_VERSION}'
}
Create a Background Service
public class PorcupineService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
in your MainActivity
, add code to start and stop the PorcupineService
:
private void startService() {
Intent serviceIntent = new Intent(this, PorcupineService.class);
ContextCompat.startForegroundService(this, serviceIntent);
}
private void stopService() {
Intent serviceIntent = new Intent(this, PorcupineService.class);
stopService(serviceIntent);
}
Request Audio Permissions
In AndroidManifest.xml
, add this:
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
Check if you have permission to record audio in the MainActivity
and if not, ask the user for it. Add the following code to achieve this:
private boolean hasRecordPermission() {
return ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
== PackageManager.PERMISSION_GRANTED;
}
private void requestRecordPermission() {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, 0);
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
if (grantResults.length == 0 ||
grantResults[0] == PackageManager.PERMISSION_DENIED) {
// handle permission denied
} else {
startService();
}
}
Launch Wake Word Engine from a Service
For this demo, we'll use one of built-in keywords, ‘Computer’. However, you can train a custom wake phrase on the Picovoice Console by signing up for free.
You also need an AccessKey
which can be found on your Picovoice Console dashboard.
In our PorcupineService
class, we’ll create an instance of PorcupineManager
to handle audio capture and processing. The service class now looks like this:
import ai.picovoice.porcupine.Porcupine;
import ai.picovoice.porcupine.PorcupineException;
import ai.picovoice.porcupine.PorcupineManager;
public class PorcupineService extends Service {
private String accessKey = "..."; // your Picovoice AccessKey
private PorcupineManager porcupineManager;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
porcupineManager = new PorcupineManager.Builder()
.setAccessKey(accessKey)
.setKeyword(Porcupine.BuiltInKeyword.COMPUTER)
.setSensitivity(0.7f)
.build(getApplicationContext(),
(keywordIndex) -> {
// wake word detected!
});
porcupineManager.start();
} catch (PorcupineException e) {
Log.e("PORCUPINE_SERVICE", e.toString());
}
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
try {
porcupineManager.stop();
porcupineManager.delete();
} catch (PorcupineException e) {
Log.e("PORCUPINE", e.toString());
}
super.onDestroy();
}
}
Voila!
Resources:
The tutorial was originally published on Medium.
Porcupine Android SDK
Tutorial Source Code
Picovoice.ai
Posted on February 24, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.