我在我的程序语音识别器中有,当我直接在 onCreate 方法中运行 startListening 方法时,语音识别器似乎不起作用。但是当我在按钮 onClick 方法中放置 startListening 方法时,语音识别器正在工作。我想直接调用语音识别器方法 startListening,如下面的程序所示。
以下是我的程序。
SpeechRecognizer mSpeechRecognizer;
Intent mSpeechRecognizerIntent;
TextView textView;
String speakId = "one";
String oldId = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_robo);
textView = (TextView) findViewById(R.id.textView);
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,
Locale.getDefault());
mSpeechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle params) {
}
@Override
public void onBeginningOfSpeech() {
}
@Override
public void onRmsChanged(float rmsdB) {
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int error) {
}
@Override
public void onResults(Bundle results) {
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
textView.setText(matches.get(0));
//displaying the first match
//if (matches != null)
//processResult(matches.get(0));
}
但是当我在按钮 onClick 事件中调用 mSpeechRecognizer.startListening(mSpeechRecognizerIntent) 时,SpeechRecognizer 正在工作。
public void Listen(View view) {
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
为什么当代码放在 onCreate 方法中而不是在 onClick 方法中时不起作用。请告诉一个解决方案。
qq_笑_17
相关分类