0
我在我的SoftKeyboard应用程序中使用SpeechRecognizer和RecognizerListener。我可以从听者的结果,但触发结果自动添加到InputConnection被证明是有问题的...继承人一些代码..将RecognizerListener的结果返回给调用它的类
public class SoftKeyboard extends InputMethodService
implements KeyboardView.OnKeyboardActionListener {
private SpeechRecognizer getMsg = SpeechRecognizer.createSpeechRecognizer(this);
public static String mResult = "";
// a bunch of code that is irrelavent to my question
public void getVoice() {
mResult = "";
context = this;
if (isListening == false) {
isListening = true;
Intent intent = new Intent (RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
Talk doMsg = new Talk();
getMsg.setRecognitionListener(doMsg);
getMsg.startListening(intent);
}
}
public void putText() {
getCurrentInputConnection().commitText(mResult, 1);
}
}
class Talk implements RecognitionListener {
Context context = SoftKeyboard.context;
public void onBeginningOfSpeech() {
Toast.makeText(context, "Beginning of Speech", Toast.LENGTH_SHORT).show();
}
public void onBufferReceived(byte[] buffer) {
//Toast.makeText(context, "Buffer received", Toast.LENGTH_SHORT).show();
}
public void onEndOfSpeech() {
Toast.makeText(context, "End Of Speech", Toast.LENGTH_SHORT).show();
}
public void onError(int error) {
// TODO Auto-generated method stub
Toast.makeText(context, "There was an Error: "+String.valueOf(error), Toast.LENGTH_SHORT).show();
SoftKeyboard.isListening = false;
}
public void onEvent(int eventType, Bundle params) {
Toast.makeText(context, "Event: "+String.valueOf(eventType), Toast.LENGTH_SHORT).show();
}
public void onPartialResults(Bundle partialResults) {
Toast.makeText(context, "Partial Results to be had", Toast.LENGTH_SHORT).show();
}
public void onReadyForSpeech(Bundle params) {
Toast.makeText(context, "Ready For Speech!", Toast.LENGTH_SHORT).show();
}
public void onResults(Bundle results) {
ArrayList<String> result = results.getStringArrayList("results_recognition");
if (result.isEmpty() == false) {
for (int i = 0; i<(result.size()-1);i++) {
Toast.makeText(context, result.get(i), Toast.LENGTH_SHORT).show();
}
SoftKeyboard.mResult = result.get(0);
} else {
Toast.makeText(context, "no results", Toast.LENGTH_SHORT).show();
SoftKeyboard.mResult = "-1";
}
SoftKeyboard.isListening = false;
SoftKeyboard.putText();
}
public void onRmsChanged(float rmsdB) {
}
}
所以我称之为从按钮的按下getVoice方法,它在SoftKeyboard中调用putText方法之前工作正常。我只能从onResult方法调用一个静态方法,但如果我让putText成为一个静态方法,我不能调用getCurrentInputConnection ...我发现的唯一方法是让用户第二次点击按钮来插入文字,但这不是非常用户友好的。我想在侦听器中的onResult方法运行后自动调用getCurrentInputConnection,但静态/非静态冲突在这里非常麻烦。任何帮助将不胜感激!谢谢!
谢谢你的建议。我试过了,它没有帮助。我的问题是,doMsg是一个静态对象,所以当我尝试从RecognitionListener doMsg中的onResults方法执行非静态方法getCurrentInputConnection()。commitText时,它是不允许的。我需要一种方法来执行静态对象的非静态命令。无论是或者我需要使doMsg非静态。我试图在doMsg设置mResult的值时实现一个监听器,但是我无法使其工作。所以我最好的解决方案是要求用户按下按钮来插入文本。 – alyon2002 2011-12-21 20:30:17