【问题标题】:SpeechRecognizer not starting语音识别器未启动
【发布时间】:2014-05-13 22:56:40
【问题描述】:

简介

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

方法

这是 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());

Activity 实现了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() {
    }

【问题讨论】:

  • 根据文档developer.android.com/reference/android/speech/… 必须从主线程调用语音识别器。对你来说是这样吗?您可能在其他线程中调用 myMethod,您应该尝试将其移至 UI 之一。在stackoverflow.com/questions/14940657/…的原始讨论中也提到了它
  • @Nikolay Shmyrev 是的,它在主线程中......我已经在 stackoverflow 中看到了所有关于这个的现有帖子,它应该可以工作......也许它与我的设备或安卓版……不知道……
  • texttospeech 监听器不是主线程。
  • @Nikolay Shmyrev 我不太明白你的意思,但是......我写的是我的活动实现的TTS,我在调用onInit时启动了main方法,但这一切都发生了在主要活动中
  • 先尝试在onCreate中不带tts的情况下初始化语音识别。

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


【解决方案1】:

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

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

或者,您可以在 TTS 线程中发布处理程序以初始化 ASR:

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

【讨论】:

  • 我现在在 onCreate() mSpeechRecognizer.startListening(mSpeechRecognizerIntent); 中有这个,但我已经禁用了我在 onCreate() tts = new TextToSpeech(this, this 中也有的 TTS 初始化。我将不胜感激关于如何做你所说的更深入的解释。我是第一次使用 TTS an Speech 并且我对你所说的 ASR 有点迷茫,我会更新 OP 让你看到我的活动结构,这也可能对你更有帮助
  • 重新启用 TTS,它应该可以工作。问题是你在 onCreate 中开始监听,不管你在哪里初始化 TTS。
  • 但是,我不得不说,如果我禁用了 TTS 初始化,我会看到语音侦听器的日志,但如果我重新启用 TTS,我看不到日志。我读过使用相同的 API,也许我必须在使用一个或另一个之前禁用/启用某些东西,因为它们不能同时使用展位?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多