【问题标题】:Speech recognition listener doesn't work in Galaxy SII语音识别侦听器在 Galaxy SII 中不起作用
【发布时间】:2013-05-07 21:13:22
【问题描述】:

我正在开发一个始终聆听用户声音的 Android 应用程序。当我在 Sony X10i 上运行它时它可以工作,但在三星 Galaxy SII 上不起作用。 这是我的代码:

    SpeechRecognizer     speechRecognizer;
    speechRecognizer = SpeechRecognizer.createSpeechRecognizer(getBaseContext());
    MyRecognitionListener speechListner=new MyRecognitionListener();
    speechRecognizer.setRecognitionListener(speechListner);
    speechRecognizer.startListening(RecognizerIntent.getVoiceDetailsIntent(getApplicationContext()));

这是我的监听类:

class MyRecognitionListener implements RecognitionListener {

    public void onBeginningOfSpeech() {
        Log.d("leapkh", "onBeginningOfSpeech");
    }

    public void onBufferReceived(byte[] buffer) {
        Log.d("leapkh", "onBufferReceived");
    }

    public void onEndOfSpeech() {
        Log.d("leapkh", "onEndOfSpeech");
    }

    public void onError(int error) {
        Log.d("leapkh", "onError");
    }

    public void onEvent(int eventType, Bundle params) {
        Log.d("leapkh", "onEvent");
    }

    public void onPartialResults(Bundle partialResults) {
        Log.d("leapkh", "onPartialResults");
    }

    public void onReadyForSpeech(Bundle params) {
        Log.d("leapkh", "onReadyForSpeech");
    }


    public void onResults(Bundle results) {
        Log.d("leapkh", "onResults");

    }

    public void onRmsChanged(float rmsdB) {
        Log.d("leapkh", "onRmsChanged");
    }
}

在这种情况下,如何解决这个问题?

【问题讨论】:

    标签: android voice-recognition


    【解决方案1】:

    我找到了解决办法。

    speechRecognizer.startListening()方法的参数改为intent如下:

        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getApplication().getPackageName());
        speechRecognizer.startListening(intent);
    

    【讨论】:

      【解决方案2】:

      更改意图传递的参数

      {
      Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
      
       intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
      
       intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
                      this.getPackageName());
      
       intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                      RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
      
       intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
      
          if (speech != null) {
                  speech = null;
      
              }
      
              SpeechRecognizer speech SpeechRecognizer.createSpeechRecognizer(this);
      
              speech.setRecognitionListener(this);
      
              speech.startListening(intent);
      

      }

      还要检查您遇到的错误类型 对于不匹配,网络和服务器错误再次调用 startListening

          public void startListening() {
          try {
      
              if (SpeechRecognizer.isRecognitionAvailable(this)) {
                  if (speech != null) {
                      speech.startListening(intent);
      
                  } else {
                      SimpleMethod();
                  }
              }
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-23
        • 1970-01-01
        • 2012-07-31
        • 1970-01-01
        • 2012-08-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多