gpt4 book ai didi

java - SpeechRecognizer 什么都不做......我错过了什么

转载 作者:行者123 更新时间:2023-11-30 10:23:22 27 4
gpt4 key购买 nike

请看下面的代码。 onBeginningOfSpeech() 被调用(甚至在我开始说话之前,顺便说一句),但随后 - 没有。我错过了什么?

我承认代码大部分是由不同的例子组成的,我没有完全理解。但我希望它至少能做点什么,这样我就可以继续试验。感谢您的帮助。

public class SpeechRecogActivity extends Activity {
private SpeechRecognizer recog;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("TTS_SpeechRecog", "Starting...");
recog = SpeechRecognizer.createSpeechRecognizer(this);
recog.setRecognitionListener(new RecogListener());
Intent intent = RecognizerIntent.getVoiceDetailsIntent(getApplicationContext());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "cmn-Hans-CN");
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "cmn-Hans-CN");
intent.putExtra(RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE, "cmn-Hans-CN");
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "com.dummy");
intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
recog.startListening(intent);
}

private static class RecogListener implements RecognitionListener {
@Override
public void onBeginningOfSpeech() {
Log.e("TTS_SpeechRecog", "onBeginningOfSpeech...");
}

@Override
public void onRmsChanged(float rmsdB) {
Log.e("TTS_SpeechRecog", "onRmsChanged...");
}

@Override
public void onEndOfSpeech() {
Log.e("TTS_SpeechRecog", "onEndOfSpeech...");
}

@Override
public void onReadyForSpeech(Bundle params) {
Log.e("TTS_SpeechRecog", "onReadyForSpeech...");
}

@Override
public void onResults(Bundle data) {
Log.e("TTS_SpeechRecog", "onResults...");
ArrayList strlist = data.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for (int i = 0; i < strlist.size();i++ ) {
Log.d("Speech", "result=" + strlist.get(i));
}
}

@Override
public void onPartialResults(Bundle partialResults) {
Log.e("TTS_SpeechRecog", "onPartialResults...");
}

@Override
public void onEvent(int eventType, Bundle params) {
Log.e("TTS_SpeechRecog", "onEvent...");
}

@Override
public void onBufferReceived(byte[] buffer) {
Log.e("TTS_SpeechRecog", "onBufferReceived...");
}

@Override
public void onError(int error) {
Log.e("TTS_SpeechRecog", "onBeginningOfSpeech...");
}
}
}

最佳答案

也许你忘了添加权限。

android.permission.RECORD_AUDIO

在 API 23 之前你可以添加:

<code><uses-permission android:name="android.permission.RECORD_AUDIO" /></code>

在 API 23 之后使用运行时权限。

关于java - SpeechRecognizer 什么都不做......我错过了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47015000/

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