【问题标题】:Android code for grammar based speech recognition用于基于语法的语音识别的 Android 代码
【发布时间】:2014-05-07 11:18:44
【问题描述】:

我正在开发一个需要语音到文本转换的 Android 应用程序。目前我已经为此目的使用谷歌语音搜索,但使用谷歌需要互联网连接,而且它会给出非常不准确的结果,例如。当我说“1”时,它会打印“何时”.. 因此,我想定义自己的语法,这样当我发出语音命令时,它会搜索我定义的语法以找到最佳匹配,而不是搜索互联网。在 Windows 8 手机上使用语法进行语音识别很容易,但我想知道如何在 Android 手机上实现这一点。

【问题讨论】:

标签: java android speech-recognition


【解决方案1】:

请看下面的代码!..

  **Using Intent:::**

        Intent intent = new Intent(
                RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

        try {
            startActivityForResult(intent, RESULT_SPEECH);
            txtText.setText("");
        } catch (ActivityNotFoundException a) {
            Toast t = Toast.makeText(getApplicationContext(),
                    "Opps! Your device doesn't support Speech to Text",
                    Toast.LENGTH_SHORT);
            t.show();
        }

不使用 Intent::

第 1 步:在您的类中实现 RecognitionListener。

第 2 步。添加以下代码:

 private SpeechRecognizer speech = null;
private Intent speechIntent=null;

/**
 * Speech Result is used to Store the Voice Commands
 */
private ArrayList<String> speechResult;


inside onCreate()  --- > 

 speech = SpeechRecognizer.createSpeechRecognizer(this);
 speech.setRecognitionListener(this);


  Trigger this after your button Click: 

       if (SpeechRecognizer.isRecognitionAvailable(this)) {
            if(speechIntent==null ){
                speechIntent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
                speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
                speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
                speechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,12);
                speech.startListening(speechIntent);
            }else{
                if(speech!=null){
                    speech.startListening(speechIntent);
                }
            }
        }

将 onResults 链接替换为:

public void onResults(Bundle results) {
speechResult = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if(speechResult!=null){
    if(speechResult.size()>0 ){
        String command=speechResult.get(0).toString();
                 }
         }

}

【讨论】:

  • 语音识别器无法识别撇号,比如你叫什么名字?它不认识,请帮助我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-13
  • 1970-01-01
  • 1970-01-01
  • 2021-08-13
  • 1970-01-01
  • 2020-11-14
相关资源
最近更新 更多