How to make a hot word detection service in android

I want to create a service that needs to listen for hot words in the background so that when I say hi it needs to invoke an activity, how can I do that about voiceInteractionService, but I read that it is not available for use, is it true? can anyone tell me how should i solve this problem? Its about a hot word detector

I followed 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
}}

      

+3


source to share





All Articles