android语音RecognitionListener不在循环中工作

时间:2015-09-24 20:21:42

标签: android speech-recognition

我为语音识别创建了一个RecognitionListener。问题是,当我运行应用程序public void onError(int errorCode)时,如果出现一些错误,例如" SpeechRecognizer.ERROR_SPEECH_TIMEOUT"。

但是当我在调试模式下运行相同的应用程序并在public void onError(int errorCode)中放置一个调试指针时,它运行得很好,并且在出现错误时始终会进行控制。语音监听器没有初始化并启动并且onready方法被调用但是停止听不知道为什么。

如果您不知道为什么在不在调试模式下运行时控件不会出现,请告诉我。

    package com.voice.java.service;

import java.util.ArrayList;
import java.util.List;

import com.voice.jarvis.CommandProcessorImpl;
import com.voice.jarvis.JARVISActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.widget.Toast;

public class CommandVoiceListner implements RecognitionListener{
    private static Context ctx;
    public CommandVoiceListner(){   }

    public CommandVoiceListner(Context ctx){
        this.ctx = ctx;
    }

    private static SpeechRecognizer speech = null;
    private static Intent recognizerIntent;
    private String LOG_TAG = "CommandVoiceListner";
    private List<String> mResults;
    private CommandProcessorImpl commandProcessorImpl;
    private boolean isRecognitionServiceAvailable = false;

    public void startListening(){
        Log.i(LOG_TAG, "context in listner : " + ctx);
        if (SpeechRecognizer.isRecognitionAvailable(ctx)) {
            if (isRecognitionServiceAvailable){//(speech!=null){
                speech.startListening(recognizerIntent);
                mResults = new ArrayList<String>();
            }
            else
                startSR();
        }
    }

    public void startSR(){
            isRecognitionServiceAvailable = true;
            speech = SpeechRecognizer.createSpeechRecognizer(ctx);
            speech.setRecognitionListener(this);
            recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,
                    "en");
            recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
                    ctx.getPackageName());
            recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
            recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
            speech.startListening(recognizerIntent);
    }


    @Override
    public void onBeginningOfSpeech() {
        Log.i(LOG_TAG, "onBeginningOfSpeech");
    }

    @Override
    public void onBufferReceived(byte[] buffer) {
        Log.i(LOG_TAG, "onBufferReceived: " + buffer);
    }

    @Override
    public void onEndOfSpeech() {
        Log.i(LOG_TAG, "onEndOfSpeech");
    }

    @Override
    public void onError(int errorCode) {
        String errorMessage = getErrorText(errorCode);
        Log.d(LOG_TAG, "FAILED " + errorMessage);
    }

    @Override
    public void onEvent(int arg0, Bundle arg1) {
        Log.i(LOG_TAG, "onEvent");
    }

    @Override
    public void onPartialResults(Bundle arg0) {
        Log.i(LOG_TAG, "onPartialResults");
    }

    @Override
    public void onReadyForSpeech(Bundle arg0) {
        Log.i(LOG_TAG, "onReadyForSpeech");
    }

    @Override
    public void onResults(Bundle results) {
        Log.d(LOG_TAG, "onResults " + results);
        isRecognitionServiceAvailable = false;
        mResults = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        Log.i(LOG_TAG, "starting activity from listner");
        commandProcessorImpl = new CommandProcessorImpl(ctx);
        try {
            //commandProcessorImpl.setmResults(mResults);
            commandProcessorImpl.filterUserInputText(mResults);
            //new Thread(commandProcessorImpl).start();
        }catch (Exception e) {
            Log.d(LOG_TAG, "error occured onResult : " + e);
        }
        startListening();
    }

    @Override
    public void onRmsChanged(float rmsdB) {
        Log.i(LOG_TAG, "onRmsChanged: " + rmsdB);
    }

    public String getErrorText(int errorCode) {
        String message;
        switch (errorCode) {
        case SpeechRecognizer.ERROR_AUDIO:
            message = "Audio recording error";
            break;
        case SpeechRecognizer.ERROR_CLIENT:
            message = "Client side error";
            startListening();
            break;
        case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
            message = "Insufficient permissions";
            break;
        case SpeechRecognizer.ERROR_NETWORK:
            message = "Network error";
            startListening();
            break;
        case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
            message = "Network timeout";
            startListening();
            break;
        case SpeechRecognizer.ERROR_NO_MATCH:
            message = "No match";
            speech.startListening(recognizerIntent);
            break;
        case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
            message = "RecognitionService busy";
            //startListening();
            break;
        case SpeechRecognizer.ERROR_SERVER:
            message = "error from server";
            startListening();
            break;
        case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
            message = "No speech input";
            startListening();
            break;
        default:
            message = "Didn't understand, please try again.";
            startListening();
            break;
        }
        return message;
    }

}

1 个答案:

答案 0 :(得分:0)

我还没有尝试过你所做的但我在使用类似于你的录音应用程序时遇到了问题(即当我用来调试应用程序它会工作时,通常它会引发异常)问题是设备依赖于记录将在准备好媒体记录器之前开始,但是当调试点被应用时,它将有时间准备媒体记录器,因此我在100毫秒的延迟后才开始记录并且它起作用 我希望这对你有所帮助。