我正在尝试在我的 Android 应用程序中使用 SpeechRecognizer 库,到目前为止,它的工作给我留下了很多问题。首先,当我停止说话时它不会停止。 如果我试图停止语音识别自己,下次它会给我“不匹配!”马上。
我的问题是:当我使用谷歌语音识别时(例如,当我在网络上搜索时),它就像一个魅力。在我的应用程序中,它远非完美,尽管库是一样的。我的实现有什么问题?
我的代码(简化版):
注意:我尝试使用部分结果来使语音识别更加灵活,但除了识别速度变快之外,我看不到任何效果。
public void setupVoiceRecognition(Activity activity) {
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(activity.getApplicationContext());
mSpeechRecognizer.setRecognitionListener(this);
mRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
activity.getPackageName());
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS,
true);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mRecognizerIntent.putExtra(EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 5000);
mRecognizerIntent.putExtra(EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 3000);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
mContext = activity.getApplicationContext();
if (mMainBtn != null) {
mMainBtn.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
VoiceRecognition.this.onClick();
}
});
}
}
public void forceStop() {
if (mListening) {
toggleListening(false);
}
}
public void onClick() {
toggleListening(!mListening);
}
private void toggleListening(boolean start) {
mPartialLength = 0;
if (start) {
mSpeechRecognizer.startListening(mRecognizerIntent);
} else {
mSpeechRecognizer.stopListening();
}
if (mMainBtn != null) {
mMainBtn.setImageResource((start) ? R.drawable.icon_record_active : R.drawable.icon_record_white);
}
if (mSupportBtn != null) {
mSupportBtn.setImageResource((start) ? R.drawable.icon_record_active : R.drawable.icon_record_white);
}
mListening = start;
}
...
@Override public void onError(int i) {
if (mListening) {
String errorText;
switch (i) {
case SpeechRecognizer.ERROR_AUDIO:
errorText = MyApp.getContext().getString(R.string.speech_recognition_err3);
break;
...
}
MyApp.showToast(errorText);
toggleListening(false);
if (i == NO_MATCH) {
toggleListening(true);
}
}
}
@Override public void onResults(Bundle bundle) {
ArrayList<String> matches = bundle
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if (matches != null) {
String fullText = mViewForText.getText().toString();
mViewForText.setText(fullText.substring(0, fullText.length() - mPartialLength) + matches.get(0) + " ");
mViewForText.requestFocus(View.FOCUS_RIGHT);
mViewForText.setSelection(mViewForText.getText().length());
mPartialLength = 0;
forceStop();
}
}
@Override public void onPartialResults(Bundle bundle) {
ArrayList<String> matches = bundle
.getStringArrayList(EXTRA_PARTIAL_RESULTS);
if (matches != null) {
mViewForText.setText(mViewForText.getText().toString() + matches.get(0) + " ");
mPartialLength += matches.get(0).length() + 1;
mViewForText.requestFocus(View.FOCUS_RIGHT);
mViewForText.setSelection(mViewForText.getText().length());
}
}
}
最佳答案
Google 通过第三方应用程序的 SpeechRecognizer 禁用了连续语音识别。我认为这是因为他们现在已经付费 API ( https://cloud.google.com/speech/ ),它运行良好但不是免费的。
关于 NO_MATCH 错误。谷歌听到自己的哔哔声邀请信号并假定这是演讲开始。如无法识别提示音则返回NO_MATCH错误。
有一个选项。您可以降级 Google 应用程序以获得更稳定的识别服务工作。 Google 应用程序的最后一个正常工作版本是 6.2.34
关于java - SpeechRecognizer 在启动后立即不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47695367/