谷歌语音识别听众不听

时间:2017-02-27 12:29:03

标签: android speech-recognition

您好我正在运行谷歌语音识别服务,但它会在几秒钟后停止监听我如何重新启动它或使其循环

    mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(getApplication());
    mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,getPackageName());


    SpeechRecognitionListener listener = new SpeechRecognitionListener();
    mSpeechRecognizer.setRecognitionListener(listener);

   CountDownTimer mTimer = new CountDownTimer(1500, 500) {
        @Override
        public void onTick(long l) {
        }

        @Override
        public void onFinish() {
            Log.d("Speech", "Timer.onFinish: Timer Finished, Restart recognizer");
            //mSpeechRecognizer.cancel();
             mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
        }
    };

mTimer.start();

1 个答案:

答案 0 :(得分:0)

删除您的CountdownTimer。把它放在一个方法中,当你想开始听时,调用方法。

try {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(getApplicationContext(),
                "Speech not supported on your device",
                Toast.LENGTH_SHORT).show();
    }

语音输出可以在onActivitySpeech方法中获得。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQ_CODE_SPEECH_INPUT) {
        if (resultCode == RESULT_OK && data != null) {
        //Handle the output
        }
    }
 }