Conrad Media
Conrad Media

Reputation: 87

Voice Listen API in background using more battery

I am developing a app in android which listens to voice all the time in background. i am using SpeechRecognizer offline api . please can any body tell me any best option . My code is :

 public void raise() {
    try {
        handler.post(new Runnable() {

            @Override
            public void run() {
                offSound();
                speech = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
                listener = new MyRecognitionListener();
                speech.setRecognitionListener(listener);
                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, SharedPreferenceWriter.getInstance(getApplicationContext()).getString(SPreferenceKey.SELECTED_LANGUAGE));
                intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getApplication().getPackageName());

                intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, 1000);
                intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 1000);
                intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 1000);

                intent.putExtra(RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE, true);
enter code here
                speech.startListening(intent);

                /*
                 * if (countPrintLog++ > 150) { countPrintLog = 0;
                 * LogManager
                 * .getInstance().writeToLog(LogManager.LOG_STORAGE_FILE,
                 * "Speech Recogniser is working"); }
                 */
                Log.i("", "Calling the Recognise");

            }
        });

    } catch (Exception e) {
        e.printStackTrace();
    }
}

class MyRecognitionListener implements RecognitionListener {

    @Override
    public void onBeginningOfSpeech() {
    }

    @Override
    public void onBufferReceived(byte[] buffer) {
    }

    @Override
    public void onEndOfSpeech() {
    }

    @Override
    public void onError(int error) {
        onSound();
        Log.i("", "onError");
        isCriticalSectionRaise = false;
    }

    @Override
    public void onEvent(int eventType, Bundle params) {
    }

    @Override
    public void onPartialResults(Bundle partialResults) {
    }

    @Override
    public void onReadyForSpeech(Bundle params) {
    }
    @Override
    public void onResults(Bundle results) {



    }

    @Override
    public void onRmsChanged(float rmsdB) {

    }

} 

This consumes a lot of battery resource and heats the device. I'm looking for a better option

Upvotes: 2

Views: 1438

Answers (1)

Nikolay Shmyrev
Nikolay Shmyrev

Reputation: 25220

Large vocabulary speech recognition requires quite a lot of resources, you need to use special solution to listen continuously.

If you are interested, take a look on CMUSphinx on Android

http://cmusphinx.sourceforge.net/wiki/tutorialandroid

Demo above can listen for keyword "oh mighty computer" efficiently, you can configure the keyword and detection threshold. On our experiment the listening takes less resources than screen and the battery easily lasts whole day.

Upvotes: 1

Related Questions