ann*_*mph 3 java android speech-recognition
我正在尝试在我的 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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
Google 通过 SpeechRecognizer 禁用了第三方应用程序的连续语音识别。我认为这是因为他们现在已经付费了 API(https://cloud.google.com/speech/),该 API 运行良好,但不是免费的。
关于NO_MATCH错误。谷歌听到自己的蜂鸣邀请信号并认为这是演讲开始。如无法识别蜂鸣声,则返回 NO_MATCH 错误。
有一个选项。您可以降级Google应用程序以使识别服务工作更稳定。Google 应用程序的最后一个正常工作版本是 6.2.34