android 如何等待语音识别结果?
How to wait for result of speech recognition in android?
我在 android 中使用识别侦听器进行语音识别。我的 class 的基本构造如下:
class SpeechInput implements RecognitionListener {
Intent intent;
SpeechRecognizer sr;
boolean stop;
public void init() {
sr = SpeechRecognizer.createSpeechRecognizer(context);
sr.setRecognitionListener(this);
intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,context.getPackageName());
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,3);
}
...
}
我陷入了一种情况,我想 运行 android 循环识别侦听器,超出以下范围:
for(int i=0; i<N; i++) {
// Some processing code
sr.startListening(intent);
}
现在,我想在它再次开始收听之前等待输出。为了实现这一点,我尝试使用如下锁定机制:
for(int i=0; i<N; i++) {
// Some processing code
sr.startListening(intent);
stop = false;
new Task().execute().get();
}
其中 Task 是定义如下的 asyncTask:
private class Task extends AsyncTask<Void,Void,Void> {
@Override
protected Void doInBackground(Void... params) {
try {
int counter = 0;
while(!stop) {
counter++;
}
} catch(Exception e) {
}
return null;
}
}
布尔值'stop'在RecognitionListener的'onResults'方法中更新如下:
public void onResults(Bundle results) {
...
stop = true;
}
问题是语音识别根本不起作用。什么都没有发生,它甚至没有开始。我猜这是因为 asyncTask 占用了所有处理器时间。你能指导我建立一个我能够实现这一目标的架构吗?谢谢你。
收到结果后重新开始收听。无需循环,无需等待。
我不是 Android 上的语音识别专家,但您可能通过调用
阻塞了主线程
new Task().execute().get();
所以语音识别可能从未启动过。
需要注意的是Android的Speech RecognitionAPI方法只能从主线程调用,所有的回调都是在应用程序的主线程上执行。
因此,当主线程被阻塞时,识别侦听器也被阻塞,这就是为什么当我尝试按照问题中所示的方式执行语音识别时没有任何反应。
从已接受的答案中得到启发,我使用 RecognitionListener
接口的 onResults
方法重新启动侦听器,如下所示:
public void onResults(Bundle results) {
...
sr.startListening(intent);
}
我在 android 中使用识别侦听器进行语音识别。我的 class 的基本构造如下:
class SpeechInput implements RecognitionListener {
Intent intent;
SpeechRecognizer sr;
boolean stop;
public void init() {
sr = SpeechRecognizer.createSpeechRecognizer(context);
sr.setRecognitionListener(this);
intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,context.getPackageName());
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,3);
}
...
}
我陷入了一种情况,我想 运行 android 循环识别侦听器,超出以下范围:
for(int i=0; i<N; i++) {
// Some processing code
sr.startListening(intent);
}
现在,我想在它再次开始收听之前等待输出。为了实现这一点,我尝试使用如下锁定机制:
for(int i=0; i<N; i++) {
// Some processing code
sr.startListening(intent);
stop = false;
new Task().execute().get();
}
其中 Task 是定义如下的 asyncTask:
private class Task extends AsyncTask<Void,Void,Void> {
@Override
protected Void doInBackground(Void... params) {
try {
int counter = 0;
while(!stop) {
counter++;
}
} catch(Exception e) {
}
return null;
}
}
布尔值'stop'在RecognitionListener的'onResults'方法中更新如下:
public void onResults(Bundle results) {
...
stop = true;
}
问题是语音识别根本不起作用。什么都没有发生,它甚至没有开始。我猜这是因为 asyncTask 占用了所有处理器时间。你能指导我建立一个我能够实现这一目标的架构吗?谢谢你。
收到结果后重新开始收听。无需循环,无需等待。
我不是 Android 上的语音识别专家,但您可能通过调用
阻塞了主线程new Task().execute().get();
所以语音识别可能从未启动过。
需要注意的是Android的Speech RecognitionAPI方法只能从主线程调用,所有的回调都是在应用程序的主线程上执行。
因此,当主线程被阻塞时,识别侦听器也被阻塞,这就是为什么当我尝试按照问题中所示的方式执行语音识别时没有任何反应。
从已接受的答案中得到启发,我使用 RecognitionListener
接口的 onResults
方法重新启动侦听器,如下所示:
public void onResults(Bundle results) {
...
sr.startListening(intent);
}