Android 如何将语音识别与我的应用程序集成

Android 如何将语音识别与我的应用程序集成,android,speech-recognition,integrate,Android,Speech Recognition,Integrate,我想在我的应用程序中集成语音识别 例如:当用户说特定的单词时,我会把它放在类似“hello”的代码中 它将从我的应用程序中捕获一张图片。 我尝试了许多关于语音识别的代码,但无法将其与我的应用程序集成 如何集成到我的应用程序中 如何制作特定的单词来进行捕获 将语音显示为文本 public class MainActivity extends Activity { private TextView txtSpeechInput; private ImageButton btnSpe

我想在我的应用程序中集成语音识别

例如:当用户说特定的单词时,我会把它放在类似“hello”的代码中 它将从我的应用程序中捕获一张图片。 我尝试了许多关于语音识别的代码,但无法将其与我的应用程序集成

  • 如何集成到我的应用程序中
  • 如何制作特定的单词来进行捕获 将语音显示为文本

    public class MainActivity extends Activity {
    
        private TextView txtSpeechInput;
        private ImageButton btnSpeak;
        private final int REQ_CODE_SPEECH_INPUT = 100;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            txtSpeechInput = (TextView) findViewById(R.id.txtSpeechInput);
            btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
    
            // hide the action bar
            getActionBar().hide();
    
            btnSpeak.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    promptSpeechInput();
                }
            });
    
        }
    
        /**
         * Showing google speech input dialog
         * */
        private void promptSpeechInput() {
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
            intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                    getString(R.string.speech_prompt));
            try {
                startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
            } catch (ActivityNotFoundException a) {
                Toast.makeText(getApplicationContext(),
                        getString(R.string.speech_not_supported),
                        Toast.LENGTH_SHORT).show();
            }
        }
    
        /**
         * Receiving speech input
         * */
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            switch (requestCode) {
            case REQ_CODE_SPEECH_INPUT: {
                if (resultCode == RESULT_OK && null != data) {
    
                    ArrayList<String> result = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    txtSpeechInput.setText(result.get(0));
                }
                break;
            }
    
            }
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    }
    
    公共类MainActivity扩展活动{
    私有文本视图txtSpeechInput;
    专用图像按钮btnSpeak;
    专用最终输入要求代码语音输入=100;
    @凌驾
    创建时受保护的void(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txtSpeechInput=(TextView)findViewById(R.id.txtSpeechInput);
    btnSpeak=(图像按钮)findViewById(R.id.btnSpeak);
    //隐藏操作栏
    getActionBar().hide();
    btnSpeak.setOnClickListener(新视图.OnClickListener(){
    @凌驾
    公共void onClick(视图v){
    promptSpeechInput();
    }
    });
    }
    /**
    *显示谷歌语音输入对话框
    * */
    私有无效promptSpeechInput(){
    意向意向=新意向(识别意向、行动、识别言语);
    intent.putExtra(识别器intent.EXTRA_语言_模型,
    识别者意图、语言、模型、自由形式);
    intent.putExtra(RecognizerIntent.EXTRA_语言,Locale.getDefault());
    intent.putExtra(识别器intent.EXTRA\u提示符,
    getString(R.string.speech_提示符);
    试一试{
    startActivityForResult(意图、请求代码、语音输入);
    }捕获(ActivityNotFoundException a){
    Toast.makeText(getApplicationContext(),
    getString(不支持R.string.speech),
    吐司。长度(短)。show();
    }
    }
    /**
    *接收语音输入
    * */
    @凌驾
    受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
    super.onActivityResult(请求代码、结果代码、数据);
    开关(请求代码){
    案例请求代码语音输入:{
    if(resultCode==RESULT\u OK&&null!=数据){
    ArrayList结果=数据
    .getStringArrayListExtra(识别器意图.额外结果);
    txtSpeechInput.setText(result.get(0));
    }
    打破
    }
    }
    }
    @凌驾
    公共布尔onCreateOptions菜单(菜单){
    //为菜单充气;这会将项目添加到操作栏(如果存在)。
    getMenuInflater().充气(R.menu.main,menu);
    返回true;
    }
    }
    

    在txtSpeechInput框中获取语音

    我想这会对你有所帮助

       public class YourClassName RecognitionListener{
        private SpeechRecognizer speech = null;
        private Intent recognizerIntent;
        private String LOG_TAG = "VoiceRecognitionActivity";
    
    
    
       @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_camera);
    
    
     speech = SpeechRecognizer.createSpeechRecognizer(this);
            speech.setRecognitionListener(this);
            recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,
                    "en");
    
            recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getApplication().getPackageName());
            recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
            recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
          speech.startListening(recognizerIntent);
    
    
    
    }
    
    
    
      @Override
        public void onReadyForSpeech(Bundle params) {
            Log.i(LOG_TAG+">"+ "onReadyForSpeech");
        }
    
        @Override
        public void onBeginningOfSpeech() {
    
            Log.i(LOG_TAG+"> "+"onBeginningOfSpeech");
    
        }
    
        @Override
        public void onRmsChanged(float rmsdB) {
            Log.i(LOG_TAG+">"+ "onRmsChanged: " + rmsdB);
        }
    
        @Override
        public void onBufferReceived(byte[] buffer) {
            Log.i(LOG_TAG+">"+ "onBufferReceived: " + buffer);
        }
    
        @Override
        public void onEndOfSpeech() {
            Log.i(LOG_TAG+">"+ "onEndOfSpeech");
    
    
        }
    
        @Override
        public void onError(int error) {
            String errorMessage = getErrorText(error);
            Log.d(LOG_TAG+">"+ "FAILED " + errorMessage);
          if(errorMessage.contains("RecognitionService busy"))
          {  speech.stopListening();
            speech.startListening(recognizerIntent);
    
    
          }else if(errorMessage.contains("No speech input")){
              speech.stopListening();
                speech.startListening(recognizerIntent);
    
            }else if(errorMessage.contains("No match")){
              speech.stopListening();
    
              speech.startListening(recognizerIntent);
            }
    //        speech.destroy();
    //        speech.startListening(recognizerIntent);
        }
    
        @Override
        public void onResults(Bundle results) {
            Log.i(LOG_TAG+">"+ "onResults");
            ArrayList<String> matches = results
                    .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            String text = "";
            for (String result : matches)
                text += result + "\n";
            if(text.contains("your Keyword here")){
                camera.takePicture(null, null, this);
                Log.v(text+"these are results");
    
            }
    
            Log.v(text+"On result");
        }
    
    
       @Override
        public void onPartialResults(Bundle partialResults) {
            Log.i(LOG_TAG+">"+ "onPartialResults");
        }
    
        @Override
        public void onEvent(int eventType, Bundle params) {
            Log.i(LOG_TAG+">"+ "onEvent");
        }
    
    
        public  String getErrorText(int errorCode) {
            String message;
            switch (errorCode) {
                case SpeechRecognizer.ERROR_AUDIO:
                    message = "Audio recording error";
                    break;
                case SpeechRecognizer.ERROR_CLIENT:
                    message = "Client side error";
                    break;
                case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
                    message = "Insufficient permissions";
                    break;
                case SpeechRecognizer.ERROR_NETWORK:
                    message = "Network error";
                    break;
                case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
                    message = "Network timeout";
                    break;
                case SpeechRecognizer.ERROR_NO_MATCH:
                    message = "No match";
    
    
                    break;
                case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
                    message = "RecognitionService busy";
    
                    break;
                case SpeechRecognizer.ERROR_SERVER:
                    message = "error from server";
                    break;
                case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
                    message = "No speech input";
    
                    break;
                default:
                    message = "Didn't understand, please try again.";
                    break;
            }
            return message;
        }
    
    }
    
    public类YourClassName RecognitionListener{
    专用语音识别器语音=null;
    私人意图识别人意图;
    私有字符串LOG_TAG=“VoiceRecognitionActivity”;
    @凌驾
    创建时受保护的void(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(窗口。功能\u无\u标题);
    setContentView(R.layout.activity_摄像头);
    SpeechRecognizer.createSpeechRecognizer(此);
    speech.setRecognitionListener(此);
    recognizerIntent=新意图(recognizerIntent.ACTION\u recognizer\u SPEECH);
    recognizerIntent.putExtra(recognizerIntent.EXTRA_语言_首选项,
    “en”);
    recognizerIntent.putExtra(recognizerIntent.EXTRA_调用_包,getApplication().getPackageName());
    recognizerIntent.putExtra(recognizerIntent.EXTRA_语言_模型,
    识别者意图、语言、模型、网络搜索);
    recognizerIntent.putExtra(recognizerIntent.EXTRA_最大_结果,3);
    演讲。听讲(识别意图);
    }
    @凌驾
    ReadyForSpeech上的公共void(Bundle参数){
    Log.i(Log_标记+“>”+“onReadyForSpeech”);
    }
    @凌驾
    开始时的公共无效fSpeech(){
    Log.i(Log_标记+“>”+“onbeginingofspeech”);
    }
    @凌驾
    在RMSCHANGED上的公共无效(浮动rmsdB){
    Log.i(Log_标记+“>”+“onRmsChanged:”+rmsdB);
    }
    @凌驾
    已接收公共无效onBufferReceived(字节[]缓冲区){
    Log.i(Log_标记+“>”+“onBufferReceived:“+buffer”);
    }
    @凌驾
    公共无效onEndOfSpeech(){
    Log.i(Log_标记+“>”+“onEndOfSpeech”);
    }
    @凌驾
    公共无效onError(内部错误){
    字符串errorMessage=getErrorText(错误);
    Log.d(日志标签+“>”+“失败”+错误消息);
    if(errorMessage.contains(“识别服务忙”))
    {speech.stop listening();
    演讲。听讲(识别意图);
    }else if(errorMessage.contains(“无语音输入”)){
    停止听;
    演讲。听讲(识别意图);
    }else if(errorMessage.contains(“不匹配”)){
    停止听;
    演讲。听讲(识别意图);
    }
    //演讲。破坏();
    //演讲。听讲(识别意图);
    }
    @凌驾
    公共结果(捆绑结果){
    Log.i(Log_标记+“>”+“onResults”);
    ArrayList匹配=结果
    .getStringArrayList(SpeechRecognitor.RESULTS_RECOGNITION);
    字符串文本=”;
    for(字符串结果:匹配项)
    文本+=结果+“\n”;
    if(text.contains(“此处为您的关键字”)){
    拍照(空,空,这个);
    Log.v(文本+“这些是结果”);
    }
    Log.v(文本+结果);
    }
    @凌驾
    public void onPartialResults(Bundle partialResults){
    Log.i(Log_标记+“>”+“onPartialResults”);
    }
    @凌驾
    public void onEvent(int eventType,Bundle参数){
    Log.i(Log_标记+“>”+“onEvent”);
    }
    公共字符串getErrorText(int errorCode){
    字符串消息;
    开关(错误代码){