设备睡眠时如何保持语音识别?

时间:2014-08-28 00:10:19

标签: android service voice-recognition

我想知道,当设备处于睡眠状态时,如何让设备通过语音识别来监听语音命令?我的想法是,我希望设备响应我的声音,即使屏幕已锁定或屏幕已超时。

这可能吗?我已经尝试将其用作服务和界面,并且一旦屏幕锁定就停止监听。我能收到任何帮助吗?这是我的班级。

public class VoiceEngineService extends Activity {

    private boolean isSpeakingDone = false; // default setting
    private SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(this);

    private AudioManager mAudioManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.wait_for_speech);

            // mute beep sound 
            mAudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);

            sr.setRecognitionListener(new listener());
            Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); // LANGUAGE_MODEL_WEB_SEARCH
            i.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getApplication()
                    .getClass().getName());
            i.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 6);
            i.putExtra(RecognizerIntent.EXTRA_PROMPT, "");
            i.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 5500);
            sr.startListening(i);
    }

    class listener implements RecognitionListener {

        @Override
        public void onReadyForSpeech(Bundle params) {
            // TODO Auto-generated method stub
        }

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

        @Override
        public void onRmsChanged(float rmsdB) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onBufferReceived(byte[] buffer) {
            // TODO Auto-generated method stub
        }

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

        @Override
        public void onError(int error) {
            // TODO Auto-generated method stub
            if (SharedPref.getVoiceController() == false) {
                sr.cancel();
                Intent i = new Intent();
                sr.startListening(i);
            } else {
                sr.stopListening();
                sr.destroy();
                finish();
            }

        }

        @Override
        public void onResults(Bundle results) {
            // TODO Auto-generated method stub
            isSpeakingDone = true;
            ArrayList<String> mDataList = results
                    .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            Intent i = new Intent();
            i.putStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS, mDataList);
            setResult(RESULT_OK, i);
            finish();
        }

        @Override
        public void onPartialResults(Bundle partialResults) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onEvent(int eventType, Bundle params) {
            // TODO Auto-generated method stub
        }

    } // end listener class

    @Override
    protected void onPause() {
        if ((isSpeakingDone == false)) {
            finish();
        }
        super.onPause();
    }

    @Override
    protected void onStop() {
        // when speaking is true finish() has already been called
        if (isSpeakingDone == false) {
            finish();
        }
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        sr.stopListening();
        sr.destroy();

        super.onDestroy();
    }
}

1 个答案:

答案 0 :(得分:2)

您必须在服务中实现语音侦听器。创建一个扩展“服务”的类,并构成一些逻辑来处理录制。

如果您已尝试过该服务,则可能是您尝试将命令重定向到最有可能被Android操作系统停止的活动。一般来说,当电话处于锁定模式时谈论做事时,你只能希望在一个或多个服务中完成任务。

当您在Activity中时,当然活动超出范围时,它将被Android操作系统关闭。但是服务仍然可以在后台运行,除非你自己的代码明确地关闭,或者在极少数情况下Android会认识到它需要内存和处理器能力用于其他任务。