Android - 语音识别限制收听时间
Android - Speech Recognition Limiting Listening Time
我正在使用 Google API 进行语音识别,但想限制收听时间。例如两秒钟。两秒钟后,即使用户继续说话,识别器也应该停止收听。我尝试了一些额外的东西,比如
EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS
EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS
EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS
但这对我没有帮助。
我的完整代码在这里,如果有人能帮助我,我将不胜感激
public void promptSpeechInput()
{
//This intent recognize the peech
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say Something");
try {
startActivityForResult(i, 100);
}
catch (ActivityNotFoundException a)
{
Toast.makeText(MainActivity.this,"Your device does not support",Toast.LENGTH_LONG).show();
}
}
//For receiving speech input
public void onActivityResult(int request_code, int result_code, Intent i)
{
super.onActivityResult(request_code, result_code, i);
switch (request_code)
{
case 100: if(result_code == RESULT_OK && i != null)
{
ArrayList<String> result = i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
resultTEXT.setText(result.get(0));
}
break;
}
}
您不能限制识别器收听的时间。只需设置他在关闭前需要收听的最短时间,而不是最大时间。
我也一直在寻找解决这个问题的方法,所以我希望你能找到更好的方法。我从另一个 Whosebug 伙伴那里找到了这个 post:
SpeechRecognizer Time Limit
在那里,他提出了解决您问题的下一个可能性:
Your best bet would be to thread some sort of timer, something like a
CountDownTimer:
yourSpeechListener.startListening(yourRecognizerIntent);
new CountDownTimer(2000, 1000) {
public void onTick(long millisUntilFinished) {
//do nothing, just let it tick
}
public void onFinish() {
yourSpeechListener.stopListening();
} }.start();
换句话说,为了让您的 SpeechRecognition 更短,您可以将下一个参数添加到您的 Intent 中:
EXTRA_PARTIAL_RESULTS
这将使您从 SpeechRecognizer 获得部分结果,这意味着您的 methoronActivityPartialResult
将 return 您另一个具有匹配值的数组。这个方法在onActivityResults之前调用,速度更快,当然不如onActivityResult精确。因此,如果您的听众正在寻找特定的词,这将对您有所帮助。
我正在做相反的工作,我需要语音监听器在手动停止之前不要被发现。我仍然没有选择使其不受限制。 (如果有人知道怎么做请告诉)。
关于你关于限制时间的问题。
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,3 );
public 静态最终字符串 EXTRA_MAX_RESULTS
可选的最大结果数限制为 return。如果省略,识别器将选择 return 的结果数量。必须是整数。
常数值:“android.speech.extra.MAX_RESULTS”
所以如果你把它设为 1,它只会接受大约 2-4 个单词等
我正在使用 Google API 进行语音识别,但想限制收听时间。例如两秒钟。两秒钟后,即使用户继续说话,识别器也应该停止收听。我尝试了一些额外的东西,比如
EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS
EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS
EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS
但这对我没有帮助。 我的完整代码在这里,如果有人能帮助我,我将不胜感激
public void promptSpeechInput()
{
//This intent recognize the peech
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say Something");
try {
startActivityForResult(i, 100);
}
catch (ActivityNotFoundException a)
{
Toast.makeText(MainActivity.this,"Your device does not support",Toast.LENGTH_LONG).show();
}
}
//For receiving speech input
public void onActivityResult(int request_code, int result_code, Intent i)
{
super.onActivityResult(request_code, result_code, i);
switch (request_code)
{
case 100: if(result_code == RESULT_OK && i != null)
{
ArrayList<String> result = i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
resultTEXT.setText(result.get(0));
}
break;
}
}
您不能限制识别器收听的时间。只需设置他在关闭前需要收听的最短时间,而不是最大时间。
我也一直在寻找解决这个问题的方法,所以我希望你能找到更好的方法。我从另一个 Whosebug 伙伴那里找到了这个 post:
SpeechRecognizer Time Limit
在那里,他提出了解决您问题的下一个可能性:
Your best bet would be to thread some sort of timer, something like a CountDownTimer:
yourSpeechListener.startListening(yourRecognizerIntent); new CountDownTimer(2000, 1000) { public void onTick(long millisUntilFinished) { //do nothing, just let it tick } public void onFinish() { yourSpeechListener.stopListening(); } }.start();
换句话说,为了让您的 SpeechRecognition 更短,您可以将下一个参数添加到您的 Intent 中: EXTRA_PARTIAL_RESULTS
这将使您从 SpeechRecognizer 获得部分结果,这意味着您的 methoronActivityPartialResult
将 return 您另一个具有匹配值的数组。这个方法在onActivityResults之前调用,速度更快,当然不如onActivityResult精确。因此,如果您的听众正在寻找特定的词,这将对您有所帮助。
我正在做相反的工作,我需要语音监听器在手动停止之前不要被发现。我仍然没有选择使其不受限制。 (如果有人知道怎么做请告诉)。
关于你关于限制时间的问题。
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,3 );
public 静态最终字符串 EXTRA_MAX_RESULTS 可选的最大结果数限制为 return。如果省略,识别器将选择 return 的结果数量。必须是整数。
常数值:“android.speech.extra.MAX_RESULTS”
所以如果你把它设为 1,它只会接受大约 2-4 个单词等