9

I have a problem with the new Android API21, specifically the VoiceInteractorService.

I would like to use the new Hotword detection in API21. If i press a button, a hotword detection for the word 'google' should start.

When I have an Activity (MainActivity) and I want to call the createAlwaysOnHotwordDetector(String keyphrase, Locale locale, AlwaysOnHotwordDetector.Callback callback) method from the VoiceInteractorService, I get an error: java.lang.IllegalStateException: Not available until onReady() is called. I tried to solve this temporarily by using a while with try catch loop to see when onReady() is called and I can excecute the createAlwaysOnHotwordDetector() method. I found out that onReady() is never called, even after letting the system loop for 15 minutes.

Does anybody have an idea how to solve this problem?

Thanks in advance.

This is my Activity for calling the VoiceInteractorService.

import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.service.voice.AlwaysOnHotwordDetector;
import android.service.voice.AlwaysOnHotwordDetector.Callback;
import android.service.voice.VoiceInteractionService;
import android.service.voice.AlwaysOnHotwordDetector.EventPayload;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    Button btn;
    VoiceInteractionService service;
    AlwaysOnHotwordDetector.Callback callback;
    Locale locale = new Locale("nl-NL");
    Context ctx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.button1);
        ctx = getApplicationContext();
        service = new VoiceInteractionService();
        btn.setOnClickListener(new OnClickListener() {
            // Create Hotword detector on button click
            @Override
            public void onClick(View v) {
                service.createAlwaysOnHotwordDetector("google", locale, callback);
            }
        });

        callback = new Callback() {

            @Override
            public void onRecognitionResumed() {
                // TODO Auto-generated method stub

            }

            @Override
            public void onRecognitionPaused() {
                // TODO Auto-generated method stub

            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub
                Log.d("error", "error");
            }

            @Override
            public void onDetected(EventPayload eventPayload) {
                // TODO Auto-generated method stub
                // Display Toast message when Hotword is detected
                Toast.makeText(ctx, "Google", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onAvailabilityChanged(int status) {
                // TODO Auto-generated method stub
            }
        };
    }
}
LHeslenfeld
  • 91
  • 1
  • 3
  • I would also like to know this. – user1810737 Nov 18 '14 at 09:55
  • Found this test app from Google, but still unclear how to call onReady. https://android.googlesource.com/platform/frameworks/base/+/fb30d6936a4793a3369fbf8bf51fa526b8a77272/tests/VoiceInteraction/src/com/android/test/voiceinteraction?autodive=0/ – LHeslenfeld Nov 18 '14 at 13:28

2 Answers2

0

VoiceInteractionService is designed to be subclassed to create your own voice interaction service. Only one such service can be enabled at a time, just like a keyboard. Once it is enabled by the user, it is active everywhere, not just in a particular activity.

You cannot use VoiceInteractionService to provide voice recognition for a particular activity.

j__m
  • 9,392
  • 1
  • 32
  • 56
0

Answer 1: AlwaysOnHotwordDetector is meant for System Apps. If you are supposed to create System Apps, you are able to give permission [android.permission.MANAGE_VOICE_KEYPHRASES] and eventually you can achieve what you want (onReady will be called).

For your better understanding, get through this example of AlwaysOnHotwordDetector in Android

Answer 2: Another way is avail to achieve is through accessing NDK.

Either you Dig & Develop to customize on your own, make SO & Say Bingo.

Or Do the following things https://github.com/Kitt-AI/snowboy/tree/master/examples/Android

Vivek
  • 113
  • 1
  • 12