【问题标题】:How to run RecognitionListener at the background of the app?如何在应用程序后台运行 RecognitionListener?
【发布时间】:2020-09-22 08:12:24
【问题描述】:

大家!我正在开发一个语音识别应用程序,该应用程序现在能够识别语音!但是,我需要在应用程序的后台运行这个语音识别代码,它必须一直听命令。对于我的应用程序,我编写了 Handler().postDelayed 函数,它计算用户登陆新活动的时间,并通过延迟 5 秒开始收听。我的问题是它只听了 2-3 秒,并且无法识别和再次听。应用运行时如何在应用后台运行语音识别?

speechRecognizer1.setRecognitionListener(new RecognitionListener() {
        @Override
        public void onReadyForSpeech(Bundle params) {


        }

        @Override
        public void onBeginningOfSpeech() {

        }

        @Override
        public void onRmsChanged(float rmsdB) {

        }

        @Override
        public void onBufferReceived(byte[] buffer) {

        }

        @Override
        public void onEndOfSpeech() {

        }

        @Override
        public void onError(int error) {

        }

        @Override
        public void onResults(Bundle bundle) {
            ArrayList<String> matches1 =bundle.getStringArrayList(speechRecognizer1.RESULTS_RECOGNITION);
            String string="";
            if(matches1!=null) {
                string = matches1.get(0);
                textView3.setText(string);
                Speak();

            }
        }



        @Override
        public void onPartialResults(Bundle bundle) {


        }

        @Override
        public void onEvent(int eventType, Bundle params) {

        }
    });

    mTTS1 = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if(status==TextToSpeech.SUCCESS){
                int result= mTTS1.setLanguage(Locale.ENGLISH);
                if(result== TextToSpeech.LANG_MISSING_DATA || result==TextToSpeech.LANG_NOT_SUPPORTED){
                    Log.e("TTS","Language Not Supported");

                }



            }
        }
    });

    new Handler().postDelayed(new Runnable(){
        public void run(){
            speechRecognizer1.startListening(intentRecognizer1);
        }
    }, 5000);

【问题讨论】:

    标签: java android speech-recognition background-application


    【解决方案1】:

    好的,你需要一个名为 Service 的 android 组件 我们在 android 中有三种类型的服务: 1.前台: 前台服务确实可以让用户注意到,例如音乐应用程序会通知正在播放的歌曲。 2.背景: 用户不会注意到此服务 3.绑定: 和绑定服务,为用户提供客户端-服务器交互

    对于您的情况,您可以使用前台服务。 决定什么更好以及如何实现它或在线程和服务之间做出决定,我建议阅读本文档: https://developer.android.com/guide/components/services

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-10
      • 2011-09-11
      • 2011-02-10
      • 2022-01-16
      • 2020-09-27
      • 1970-01-01
      相关资源
      最近更新 更多