如何在我的 Android 应用程序中添加 Continues 语音识别?
How to add Continues Speech Recognition in my Android Application?
我正在尝试在我的 Android 应用程序中实施 Continues Speech Recognition。我遵循了这个 Link 编码。这个 Continues Speech Recognition 在两天前工作。但现在语音识别效果不佳,语音收听需要更多时间。如何解决这个问题。请指导我。谢谢
识别编码:
// starts the service
protected void startListening() {
try {
initSpeech();
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
if (!intent.hasExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE))
{
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
"com.dummy");
}
sr.startListening(intent);
} catch(Exception ex) {
Log.d("SpeechRecognitionService", "Bei der Initialisierung des SpeechRecognizers ist ein Fehler aufgetreten");
}
}
// stops the service
protected void stopListening() {
if (sr != null) {
sr.stopListening();
sr.cancel();
sr.destroy();
}
sr = null;
}
protected void initSpeech() {
if (sr == null) {
sr = SpeechRecognizer.createSpeechRecognizer(this);
if (!SpeechRecognizer.isRecognitionAvailable(context)) {
Toast.makeText(context, "Speech Recognition is not available",
Toast.LENGTH_LONG).show();
finish();
}
sr.setRecognitionListener(VoiceRecognitionListener.getInstance());
}
}
用户开始说话
public void onBeginningOfSpeech() {
System.out.println("Starting to listen");
}
public void onBufferReceived(byte[] buffer) { }
// User finished speaking
public void onEndOfSpeech() {
System.out.println("Waiting for result...");
}
我找到了减少说话和收到结果之间的时间的解决方案。
请求部分结果,因为这些结果在完整结果之前交付。
我使用了这些 EXTRAS:-
mRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, Locale.getDefault().getLanguage().trim());
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 100);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
mSongSpeechRecognitionListener = new SongSpeechRecognitionListener(mRippleBackground, mFloatingActionButton);
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(mSongSpeechRecognitionListener);
然后是部分结果
public void onPartialResults(final Bundle partialResults) {
Log.i(LOG_TAG, "onPartialResults()");
final List<String> partialResultList = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for (final String result : partialResultList) {
if (result.isEmpty()) {
} else {
mPartialResults++;
if (mPartialResults == mPartialResultsMAX) {
Log.i(LOG_TAG, "onPartialResults() EXECUTE");
mFloatingActionButton.setEnabled(true);
mAsyncTask.execute(result);
break;
}
}
}
}
我将 mPartialResultsMAX
设置为 2,因为第一个部分结果似乎只有一个单词
当您收到部分结果时,您可能想要取消语音识别器。
我正在尝试在我的 Android 应用程序中实施 Continues Speech Recognition。我遵循了这个 Link 编码。这个 Continues Speech Recognition 在两天前工作。但现在语音识别效果不佳,语音收听需要更多时间。如何解决这个问题。请指导我。谢谢
识别编码:
// starts the service
protected void startListening() {
try {
initSpeech();
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
if (!intent.hasExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE))
{
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
"com.dummy");
}
sr.startListening(intent);
} catch(Exception ex) {
Log.d("SpeechRecognitionService", "Bei der Initialisierung des SpeechRecognizers ist ein Fehler aufgetreten");
}
}
// stops the service
protected void stopListening() {
if (sr != null) {
sr.stopListening();
sr.cancel();
sr.destroy();
}
sr = null;
}
protected void initSpeech() {
if (sr == null) {
sr = SpeechRecognizer.createSpeechRecognizer(this);
if (!SpeechRecognizer.isRecognitionAvailable(context)) {
Toast.makeText(context, "Speech Recognition is not available",
Toast.LENGTH_LONG).show();
finish();
}
sr.setRecognitionListener(VoiceRecognitionListener.getInstance());
}
}
用户开始说话
public void onBeginningOfSpeech() {
System.out.println("Starting to listen");
}
public void onBufferReceived(byte[] buffer) { }
// User finished speaking
public void onEndOfSpeech() {
System.out.println("Waiting for result...");
}
我找到了减少说话和收到结果之间的时间的解决方案。
请求部分结果,因为这些结果在完整结果之前交付。
我使用了这些 EXTRAS:-
mRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, Locale.getDefault().getLanguage().trim());
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 100);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
mSongSpeechRecognitionListener = new SongSpeechRecognitionListener(mRippleBackground, mFloatingActionButton);
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(mSongSpeechRecognitionListener);
然后是部分结果
public void onPartialResults(final Bundle partialResults) {
Log.i(LOG_TAG, "onPartialResults()");
final List<String> partialResultList = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for (final String result : partialResultList) {
if (result.isEmpty()) {
} else {
mPartialResults++;
if (mPartialResults == mPartialResultsMAX) {
Log.i(LOG_TAG, "onPartialResults() EXECUTE");
mFloatingActionButton.setEnabled(true);
mAsyncTask.execute(result);
break;
}
}
}
}
我将 mPartialResultsMAX
设置为 2,因为第一个部分结果似乎只有一个单词
当您收到部分结果时,您可能想要取消语音识别器。