首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于基于语法的语音识别的Android代码

用于基于语法的语音识别的Android代码
EN

Stack Overflow用户
提问于 2014-05-07 11:18:44
回答 1查看 644关注 0票数 1

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-07 11:34:14

请看下面的代码!

代码语言:javascript
复制
  **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();
        }

不使用意图:

步骤1:在类中实现RecognitionListener。

步骤2.添加以下代码:

代码语言:javascript
复制
 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链接如下:

代码语言:javascript
复制
public void onResults(Bundle results) {
speechResult = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if(speechResult!=null){
    if(speechResult.size()>0 ){
        String command=speechResult.get(0).toString();
                 }
         }

}

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23516362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档