SpeechRecognizer无法启动

时间:2014-05-13 08:40:33

标签: android speech-recognition voice-recognition recognizer-intent

引言

我需要在代码中实现语音识别。我按照其他帖子和一些教程来了解它,但它并不适合我。

APPROACH

这是onCreate中用于初始化它的代码:

Log.d("SPEECH", "speech recognition available: " + SpeechRecognizer.isRecognitionAvailable(this));
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(mRecognitionListener);

mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
        this.getPackageName());

活动实现了TextToSpeech.OnInitListener,所以,当这个实现在它自己的方法中初始化时,我调用main方法:

@Override
public void onInit(int status) {
    myMethod();
}

然后,在myMethod()里面,我开始这样的语音识别:

mSpeechRecognizer.startListening(mSpeechRecognizerIntent);

最后,这是结果的倾听者:

private final RecognitionListener mRecognitionListener = new RecognitionListener() {
    @Override
    public void onBufferReceived(byte[] buffer) {
        Log.d("SPEECH", "onBufferReceived");
    }
    @Override
    public void onError(int error) {
        Log.d("SPEECH", "onError: " + error);

        mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
    }
    @Override
    public void onEvent(int eventType, Bundle params) {
        Log.d("SPEECH", "onEvent");
    }
    @Override
    public void onPartialResults(Bundle partialResults) {
        Log.d("SPEECH", "onPartialResults");
    }
    @Override
    public void onReadyForSpeech(Bundle params) {
        Log.d("SPEECH", "onReadyForSpeech");
    }
    @Override
    public void onResults(Bundle results) {
        Log.d("SPEECH", "onResult");

        matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    }
    @Override
    public void onRmsChanged(float rmsdB) {
        Log.d("SPEECH", "onRmsChanged");
    }
    @Override
    public void onBeginningOfSpeech() {
        Log.d("SPEECH", "onBeginningOfSpeech");
    }
    @Override
    public void onEndOfSpeech() {
        Log.d("SPEECH", "onEndOfSpeech");
    }
};

当我做mSpeechRecognizer.startListening(mSpeechRecognizerIntent);它没有显示错误或没有错误的logcat,但是监听器没有初始化,我没有看到LogCat中的LOG,所以我认为它没有被很好地启动。

也许我没有好好开始听众或可能发生的事情?

更新 - 活动结构

public class GameActivity extends Activity implements TextToSpeech.OnInitListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        tts = new TextToSpeech(this, this);

        Log.d("SPEECH", "speech recognition available: " + SpeechRecognizer.isRecognitionAvailable(this));
        mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
        mSpeechRecognizer.setRecognitionListener(new SpeechListener());

        mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
            this.getPackageName());

        mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
    }

    /*Method implemented by texttospeech*/
    @Override
    public void onInit(int status) {

        if (status == TextToSpeech.SUCCESS) {
            /*set Language*/
            tts.setLanguage(Locale.getDefault());

            /*STARTS MAIN METHOD*/
            SpeechWhenMotion();
        } else {
            Log.e("TTS", "Initilization Failed");
        }
    }

    /*Main method, does all the work*/
    public void SpeechWhenMotion() {
    }

1 个答案:

答案 0 :(得分:2)

根据documentation语音识别器必须从主线程调用。您正尝试从TTS引擎启动onInit回调中的识别器。这不是主线程,tts引擎回调是在一个单独的线程中执行的。

您需要在主线程中运行ASR初始化程序,您可以先在onCreate()方法中初始化语音识别器,然后初始化文本到语音。

或者,您可以在TTS线程中将处理程序发布到init ASR:

handler.postDelayed(new Runnable() {
   @Override
   run() {
      MyASRInit()
   }
}