是否可以停止正在听用户讲话的意图?例如我有这个监听器:
@Override
public boolean onTouch(View v, MotionEvent event) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "es");
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
return true;
case MotionEvent.ACTION_UP:
//Code to stop listening user speech
return true;
}
我的想法是用户必须不断按下特定按钮,以便应用程序收听语音,就像 Whatsapp 中的麦克风按钮一样。
编辑
我想我已经尝试过@brandall 告诉我要做的事情。下面是代码的修改:
public boolean onTouch(View v, MotionEvent event) {
SpeechRecognizer speechRecognizer = createSpeechRecognizer(context);
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "es");
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
speechRecognizer.startListening(intent);
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
return true;
case MotionEvent.ACTION_UP:
speechRecognizer.stopListening();
return true;
}
return false;
}
});