请参阅下面的代码。 onBeginningOfSpeech()被调用(甚至在我开始说话之前,BTW),然后 - 没有。我错过了什么?
我承认代码主要由不同的例子组成,我不完全理解。但我希望它至少可以做一些事情,以便我可以继续进行实验。任何帮助表示赞赏。
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...");
}
}
}
答案 0 :(得分:2)
也许您忘了添加权限。
android.permission.RECORD_AUDIO
在API 23之前你可以添加:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
在API 23之后使用运行时权限。
答案 1 :(得分:-1)
点击任何按钮或小部件后,您必须启动SpeechRecognizer。
我建议你在github存储库下面使用。
https://github.com/gotev/android-speech
同时确保您在清单中添加了权限,例如:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />