gpt4 book ai didi

android - 如何在我的 Android 应用程序中添加 Continues 语音识别?

转载 作者:搜寻专家 更新时间:2023-11-01 08:34:04 24 4
gpt4 key购买 nike

我正在尝试在我的 Android 应用程序中实现连续语音识别。我已经关注了这个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 - 如何在我的 Android 应用程序中添加 Continues 语音识别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38179290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com