将RecognizerListener的结果返回给调用它的类

时间:2011-12-20 19:55:50

标签: android speech-recognition

我在SoftKeyboard应用程序中使用SpeechRecognizer和RecognizerListener。我可以从侦听器获得结果但是触发结果自动添加到InputConnection会产生问题......这是一些代码..

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,但静态/非静态冲突在这里非常麻烦。任何帮助将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:0)

尝试添加EXTRA_PARTIAL_RESULTS。这有帮助吗?