如何在我的自定义键盘中添加语音识别

时间:2014-02-13 10:56:18

标签: android

我正在开发一个工作正常的自定义软键盘。现在我想添加语音识别功能(STT),我正在使用android自定义api.But问题是,当我按下麦克风按钮语音识别器出现了,我得到了与语音相对应的字符串。但是如何在onkey事件中再次发送它以进行打印。我不知道。请帮助我。 这是代码:

public void onKey(int pc, int[] key) {
        ic = getCurrentInputConnection();
        al.clear();
        //cv.clear();
        switch (pc) {
        case 45:
                    Speech.i=false;
        Speech.ic=ic;
            Intent intent = new Intent(getApplicationContext(),Speech.class);
            intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            getCurrentInputConnection().commitText(Speech.spoken, 1);
}

这里是Speech class: -

public class Speech extends Activity {
    static ArrayList<String> results;
    static float[] confidence;
    public static String spoken;
    public static boolean i = true;
    public static InputConnection ic;
    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        // Specify free form input
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                "or forever hold your peace");
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.ENGLISH);
        startActivityForResult(intent, 5);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 5 && resultCode == RESULT_OK) {

            results = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

            String confidenceExtra = RecognizerIntent.EXTRA_CONFIDENCE_SCORES;
            confidence = data.getFloatArrayExtra(confidenceExtra);
            // TODO Do something with the recognized voice strings
        }

        spoken = null;
        spoken=getWord();
        Main.ic.commitText(spoken, 1);
        //Main m =new Main();
        //m.onKey(28, key)
        //broadcastIntent();
        finish();

    }

    public  String getWord() {
        float max = confidence[0];
        int j = 0;
        for (int i = 0; i < results.size(); i++) {
            if (max < confidence[i]) {
                j = i;
                max = confidence[i];
            }
        }
        //Log.i("main", "" + spoken);
        i = true;
        spoken=results.get(j);
        Log.i("main", "" + spoken);
        return spoken;
    }
/*  public void broadcastIntent()
    {
        Intent intent =new Intent();
        intent.setAction("ttsdone");
        sendBroadcast(intent);
    }*/

}

1 个答案:

答案 0 :(得分:0)

我猜你现在已经解决了这个问题,但是我会继续发布,以防它帮助别人。我解决这个问题的方法是将文本字符串保存在SharedPreferences中,然后在IME类的onStartInput方法中检索该字符串。可能有更好的方法来做到这一点,但这是我能想到的。我没有使用键码来提交字符串,我使用了commitText方法,如下所示:

 getCurrentInputConnection().commitText("the spoken string here", 1);