DEV Community

Dilek Karasoy for Picovoice

Posted on

No More "Hey Google"! Add your Wake Phrase to an Android app

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()
}
Enter fullscreen mode Exit fullscreen mode

Add the following reference to your app’s

build.gradle
file:

dependencies {
  implementation 'ai.picovoice:porcupine-android:${LATEST_VERSION}'
}
Enter fullscreen mode Exit fullscreen mode

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();
    }
}
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

Request Audio Permissions
In AndroidManifest.xml, add this:

<uses-permission android:name="android.permission.RECORD_AUDIO"/>
Enter fullscreen mode Exit fullscreen mode

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();
    }
}
Enter fullscreen mode Exit fullscreen mode

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 PorcupineManagerto 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();
  }
}
Enter fullscreen mode Exit fullscreen mode

Voila!

Resources:
The tutorial was originally published on Medium.
Porcupine Android SDK
Tutorial Source Code
Picovoice.ai

Top comments (1)

Collapse
 
citronbrick profile image
CitronBrick • Edited

You can enable syntax highlighting for your Dev.to posts by,

(triple backtick) (language name: eg: Java, yaml)
(your code)
(triple backtick)