语音侦听API在后台使用更多电池

时间:2014-09-08 16:31:37

标签: android background listener speech-recognition voice-recognition

我正在开发一个Android应用程序,它在后台一直监听语音。我正在使用SpeechRecognizer脱机api。请任何机构告诉我任何最佳选择。我的代码是:

 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) {

    }

} 

这会消耗大量电池资源并加热设备。我正在寻找更好的选择

1 个答案:

答案 0 :(得分:1)

大词汇量语音识别需要相当多的资源,你需要使用特殊的解决方案来持续聆听。

如果您有兴趣,请查看Android上的CMUSphinx

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

上面的演示可以有效地监听关键词“哦强大的电脑”,你可以配置关键字和检测阈值。在我们的实验中,聆听所占用的资源少于屏幕,电池可以轻松地持续一整天。

相关问题