2

我想在我的 Android 应用程序中引入永久语音识别功能。

我知道语音识别在 Android 4.1.1 和 4.2 上会不时冻结,因此我建立了一个计时器,并且不时检查语音识别是否仍然存在,如果它是不活着我停止它然后启动它。不幸的是,当它尝试重新启动语音识别器时,我得到如下信息:

SpeechRecognizer not connected to the recognition service

在 onError 回调中,我收到错误 8(文档说:ERROR_RECOGNIZER_BUSY),即使我在启动语音识别器之前停止了每个计时器。

代码如下所示:

public class MainActivity extends Activity {
private SpeechRecognizer mSpeechRecognizer;
private RecognitionListener mRecognitionListener;

private Intent mi;
private boolean isSpeechRecognizerAlive;
Timer myTimer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mi = new Intent(getApplicationContext(), MyService.class);

    mRecognitionListener = new RecognitionListener(){

        @Override
        public void onRmsChanged(float rmsdB) {
        }

        @Override
        public void onResults(Bundle results) {
            Log.e("recognizer listener", "onResults");

            ArrayList<String> result = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            for(int i=0;i<result.size();i++){
                Log.e(String.valueOf(i), result.get(i));
            }

            startRecognition(new View(getApplicationContext()));
        }

        @Override
        public void onReadyForSpeech(Bundle params) {
            Log.e("recognizer listener", "onReadyForSpeech");
        }

        @Override
        public void onPartialResults(Bundle partialResults) {
            Log.e("recognizer listener", "onPartialResults");
        }

        @Override
        public void onEvent(int eventType, Bundle params) {
            Log.e("recognizer listener", "onEvent");
        }

        @Override
        public void onError(int error) {
            Log.e("recognizer listener", "onError: " + String.valueOf(error));

            isSpeechRecognizerAlive = false;
        }

        @Override
        public void onEndOfSpeech() {
            Log.e("recognizer listener", "onEndOfSpeech");
        }

        @Override
        public void onBufferReceived(byte[] buffer) {
            Log.e("recognizer listener", "onBufferReceived");
        }

        @Override
        public void onBeginningOfSpeech() {
            Log.e("recognizer listener", "onBeginningOfSpeech");
            isSpeechRecognizerAlive = true;
        }

    };

    new CheckRecognizer().execute("");
}

private void checkIfRecognizerAslive(){
    Log.e("check", "check");
    if(!isSpeechRecognizerAlive){
        Log.e("check1", "check1");
        stopRecognition(new View(getApplicationContext()));
        startRecognition(new View(getApplicationContext()));
    }
}

@Override
protected void onResume() {
    super.onResume();
    Log.e("Start", "service");
}

@Override
protected void onPause() {
    super.onPause();
    Log.e("Stop", "service");
}


public void startRecognition(View view){
    Log.e("MainActivity", "startRecognition");
    isSpeechRecognizerAlive = false;
    mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
    mSpeechRecognizer.setRecognitionListener(mRecognitionListener);
    mSpeechRecognizer.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));
}

public void stopRecognition(View view){
    Log.e("MainActivity", "stopRecognition");
    if(mSpeechRecognizer != null){
        mSpeechRecognizer.stopListening();
        mSpeechRecognizer.cancel();
        mSpeechRecognizer.destroy();
        mSpeechRecognizer = null;
    }
}

private class CheckRecognizer extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {

        myTimer = new Timer();
        myTimer.schedule(new TimerTask() {          
            @Override
            public void run() {
                publishProgress();
            }

        }, 0, 4000);


        return "";
    }

    @Override
    protected void onPostExecute(String result) {

    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(Void... values) {
        checkIfRecognizerAslive();
    }
}

}

我真的不知道我做错了什么。你能帮我找到问题吗?或者有人知道为什么我的语音识别器很忙,即使我破坏了它?欢迎任何建议。如果您对如何在 Android 上进行永久语音识别有其他建议,那就太好了。非常感谢!

4

0 回答 0