How to make hotword detection service in android
Asked Answered
S

1

7

I want to create a service that should listens for hotword in background such that when i say hello it should invoke an activity, how can i do this, about voiceInteractionService but I have read that Its not available to use, is it true? could anyone tell me the way i should solve this problem? Its about hotword detector

I have been following this

Tried this:

public class InteractionService extends VoiceInteractionService {

static final String TAG = "InteractionService" ;
private AlwaysOnHotwordDetector mHotwordDetector;

@Override
public void onCreate() {
    super.onCreate();

    Log.i(TAG, "service started");
}

@Override
public void onReady() {
    super.onReady();
    Log.i(TAG, "Creating " + this);

    mHotwordDetector = createAlwaysOnHotwordDetector("Hello"
,  Locale.forLanguageTag("en-US"), mHotwordCallback);
    Log.i(TAG, "onReady");
}

private final AlwaysOnHotwordDetector.Callback mHotwordCallback = 
new AlwaysOnHotwordDetector.Callback() {
           @Override
           public void onAvailabilityChanged(int status) {
                    Log.i(TAG, "onAvailabilityChanged(" + status + ")");
                    hotwordAvailabilityChangeHelper(status);
                }

                    @Override
            public void onDetected(AlwaysOnHotwordDetector
               .EventPayload eventPayload) {
                    Log.i(TAG, "onDetected");
                }

                    @Override
            public void onError() {
                    Log.i(TAG, "onError");
                }

                    @Override
            public void onRecognitionPaused() {
                    Log.i(TAG, "onRecognitionPaused");
                }

                    @Override
            public void onRecognitionResumed() {
                    Log.i(TAG, "onRecognitionResumed");
                }
        };

private void hotwordAvailabilityChangeHelper(int status) {

    Log.i(TAG, "Hotword availability = " + status);
    switch (status) {
        case AlwaysOnHotwordDetector.STATE_HARDWARE_UNAVAILABLE:
            Log.i(TAG, "STATE_HARDWARE_UNAVAILABLE");
            break;
        case AlwaysOnHotwordDetector.STATE_KEYPHRASE_UNSUPPORTED:
            Log.i(TAG, "STATE_KEYPHRASE_UNSUPPORTED");
            break;
        case AlwaysOnHotwordDetector.STATE_KEYPHRASE_UNENROLLED:
            Log.i(TAG, "STATE_KEYPHRASE_UNENROLLED");
            Intent enroll = mHotwordDetector.createEnrollIntent();
            Log.i(TAG, "Need to enroll with " + enroll);
            break;
        case AlwaysOnHotwordDetector.STATE_KEYPHRASE_ENROLLED:
            Log.i(TAG, "STATE_KEYPHRASE_ENROLLED - starting recognition");
            if (mHotwordDetector.startRecognition(0)) {
                Log.i(TAG, "startRecognition succeeded");
            } else {
                Log.i(TAG, "startRecognition failed");
            }
            break;
    }

    //    final static AlwaysOnHotwordDetector.Callback
}}
Scorch answered 3/4, 2017 at 6:24 Comment(4)
Hi, Did you get answer for this?Ivaivah
Not yet you can post new question, i will help you thereScorch
Hi! I posted new question #43372313. Please do the needfulIvaivah
Okau i will seeScorch
N
-2

Porcupine's service demo does exactly this.

Northumbrian answered 29/4, 2021 at 13:22 Comment(1)
No it does not, the op requested help for the official way to set up voice assistant, maybe to replace google own assistant, or bixby on samsung. Porcupine does not replace such things.Greenhorn

© 2022 - 2024 — McMap. All rights reserved.