speechRecognizer不听

时间:2018-09-10 07:14:56

标签: java android audio-recording speech-to-text

即使我使用了startListening()方法,speechRecognizer也不起作用,它也不听

接下来是语音识别器初始化:

i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
i.putExtra(EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 20000);
i.putExtra(EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, 10000);
i.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "voice.recognition.test");
mySpeechRecognizer.startListening(i);

接下来是实现的方法:

private void initializeSpeechRecognizer() {
    if (SpeechRecognizer.isRecognitionAvailable(this)) {
        mySpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
        mySpeechRecognizer.setRecognitionListener(new RecognitionListener() {
            @Override
            public void onReadyForSpeech(Bundle bundle) {
                Toast.makeText(TestActivity.this, "Ready to hear", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onBeginningOfSpeech() {

            }

            @Override
            public void onRmsChanged(float v) {

            }

            @Override
            public void onBufferReceived(byte[] bytes) {

            }

            @Override
            public void onEndOfSpeech() {

            }

            @Override
            public void onError(int i) {

            }

            @Override
            public void onResults(Bundle bundle) {
                List<String> result = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                processResult(result.get(0));
            }

            @Override
            public void onPartialResults(Bundle bundle) {

            }

            @Override
            public void onEvent(int i, Bundle bundle) {

            }
        });
    }
}

此处processResult(result.get(0))方法处理从onResults()获得的结果

我也已授予RECORD_AUDIO权限,但这不起作用。请帮忙。

0 个答案:

没有答案