首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在android手机中使用语音识别而不显示烦人的对话框

如何在android手机中使用语音识别而不显示烦人的对话框
EN

Stack Overflow用户
提问于 2011-06-11 23:56:44
回答 3查看 75.7K关注 0票数 126

在不修改android API的情况下可以做到吗?我找到了一篇关于这方面的文章。有一条评论说我应该对android API进行修改。但它没有说明如何进行修改。有谁能给我一些如何做到这一点的建议?谢谢!

我找到了这篇文章;SpeechRecognizer他的需求几乎和我的一样。这对我来说是一个很好的参考资料!

我完全解决了这个问题。

我用谷歌搜索了一个有用的示例代码from this China website这是我的源代码

代码语言:javascript
复制
package voice.recognition.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import android.util.Log;



public class voiceRecognitionTest extends Activity implements OnClickListener 
{

   private TextView mText;
   private SpeechRecognizer sr;
   private static final String TAG = "MyStt3Activity";
   @Override
   public void onCreate(Bundle savedInstanceState) 
   {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Button speakButton = (Button) findViewById(R.id.btn_speak);     
            mText = (TextView) findViewById(R.id.textView1);     
            speakButton.setOnClickListener(this);
            sr = SpeechRecognizer.createSpeechRecognizer(this);       
            sr.setRecognitionListener(new listener());        
   }

   class listener implements RecognitionListener          
   {
            public void onReadyForSpeech(Bundle params)
            {
                     Log.d(TAG, "onReadyForSpeech");
            }
            public void onBeginningOfSpeech()
            {
                     Log.d(TAG, "onBeginningOfSpeech");
            }
            public void onRmsChanged(float rmsdB)
            {
                     Log.d(TAG, "onRmsChanged");
            }
            public void onBufferReceived(byte[] buffer)
            {
                     Log.d(TAG, "onBufferReceived");
            }
            public void onEndOfSpeech()
            {
                     Log.d(TAG, "onEndofSpeech");
            }
            public void onError(int error)
            {
                     Log.d(TAG,  "error " +  error);
                     mText.setText("error " + error);
            }
            public void onResults(Bundle results)                   
            {
                     String str = new String();
                     Log.d(TAG, "onResults " + results);
                     ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                     for (int i = 0; i < data.size(); i++)
                     {
                               Log.d(TAG, "result " + data.get(i));
                               str += data.get(i);
                     }
                     mText.setText("results: "+String.valueOf(data.size()));        
            }
            public void onPartialResults(Bundle partialResults)
            {
                     Log.d(TAG, "onPartialResults");
            }
            public void onEvent(int eventType, Bundle params)
            {
                     Log.d(TAG, "onEvent " + eventType);
            }
   }
   public void onClick(View v) {
            if (v.getId() == R.id.btn_speak) 
            {
                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,"voice.recognition.test");

                intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); 
                     sr.startListening(intent);
                     Log.i("111111","11111111");
            }
   }
}

请务必在调试后删除恼人的日志!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-06-12 00:18:40

使用SpeechRecognizer接口。您的应用程序需要具有RECORD_AUDIO权限,然后您可以创建一个SpeechRecognizer,给它一个RecognitionListener,然后调用它的startListening方法。当语音识别器准备好开始监听语音时,以及当它接收语音并将其转换为文本时,您将获得对侦听器的回调。

票数 73
EN

Stack Overflow用户

发布于 2012-06-11 17:20:47

GAST有一个方便的抽象类,您可以使用它来使用SpeechRecognizer类,只需很少的新代码。还有一个使用thisthisSpeechRecognizer作为后台服务运行的示例

票数 7
EN

Stack Overflow用户

发布于 2013-07-21 08:30:33

感谢你贴出这篇文章!我发现在oncreate中定义onclick监听器很有帮助:

代码语言:javascript
复制
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mText = (TextView) findViewById(R.id.textView1);     
    MyRecognitionListener listener = new MyRecognitionListener();
    sr = SpeechRecognizer.createSpeechRecognizer(this);       
    sr.setRecognitionListener(listener);

    findViewById(R.id.button1).setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) 
        {
                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);    
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
                intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,1); 
                intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");
                sr.startListening(intent);
        }
    });     
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6316937

复制
相关文章

相似问题

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