我正在尝试制作一个示例应用程序来演示逐字识别。到目前为止,我能够实现识别已经说过的内容的语音识别,但我希望我的应用程序开始识别和输入单词,而不是等待用户完成短语或行。我知道它在 Android 中是可能的,因为我一直在我的短信应用程序中使用它。到目前为止,我发现的教程显示了我不想在我的应用程序中使用的 Google Dialog。
任何帮助将非常感激。
到目前为止,这是我的代码,它显示了用户刚刚所说的内容,没有任何问题:-
public class SpeechActivity extends Activity implements RecognitionListener, View.OnClickListener {
private EditText editText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText)findViewById(R.id.abc);
findViewById(R.id.bca).setOnClickListener(this);
}
@Override
public void onReadyForSpeech(Bundle params) {
Log.e("Speech", "Ready");
}
@Override
public void onBeginningOfSpeech() {
Log.e("Speech", "Begin");
}
@Override
public void onRmsChanged(float rmsdB) {
Log.e("Speech", "Rms");
}
@Override
public void onBufferReceived(byte[] buffer) {
Log.e("Speech", "Buffer Received");
}
@Override
public void onEndOfSpeech() {
Log.e("Speech", "End");
}
@Override
public void onError(int error) {
Log.e("Speech", "Error");
}
@Override
public void onResults(Bundle results) {
Log.e("Speech", "Result");
if(results != null) {
Log.e("Speech", results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION).toString());
ArrayList<String> result = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
editText.setText(result.get(0));
}
}
@Override
public void onPartialResults(Bundle partialResults) {
Log.e("Speech", "Partial Result");
if(partialResults != null) {
Log.e("Speech", partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION).toString());
ArrayList<String> result = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
editText.setText(result.get(0));
}
}
@Override
public void onEvent(int eventType, Bundle params) {
Log.e("Speech", "OnEvent");
}
@Override
public void onClick(View v) {
SpeechRecognizer speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "AndroidBite Voice Recognition...");
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
speechRecognizer.setRecognitionListener(this);
speechRecognizer.startListening(intent);
}
}
但正如我所说,我希望我的应用程序能够逐字识别语音,我不希望它等到用户说完。
请帮忙!!