所有人!我正在开发一个用于语音识别的应用程序,该应用程序现在能够识别语音!然而,我需要在应用程序的后台运行这个语音识别代码,它必须一直听命令。对于我的应用程序,我编写了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);发布于 2020-06-03 23:16:54
好的,您需要一个名为Service的android组件,我们在android中有三种类型的服务: 1.Forground:一个前台服务,它做的工作对用户来说是显而易见的,例如,一个音乐应用程序,它具有关于正在播放的歌曲的通知。2.背景:此服务对用户并不明显。3.绑定:为用户提供客户端-服务器交互的绑定服务
对于您的情况,您可以使用前台服务。要决定哪个更好以及如何实现它,或者在线程和服务之间做出选择,我建议阅读以下文档:https://developer.android.com/guide/components/services
https://stackoverflow.com/questions/62176066
复制相似问题