首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在自定义键盘中添加语音识别

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

Stack Overflow用户
提问于 2014-02-13 18:56:18
回答 1查看 629关注 0票数 3

我正在开发一个自定义软键盘,这是工作的fine.Now我想添加语音识别(STT)的功能,我正在使用安卓自定义api.But的问题是,当我按下麦克风按钮,语音识别器出现,我得到的字符串对应的voice.But如何我可以再次发送它在onkey事件打印它。我没有idea.Please帮助我。代码如下:

代码语言:javascript
复制
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);
}

这是演讲课:

代码语言:javascript
复制
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);
    }*/

}
EN

回答 1

Stack Overflow用户

发布于 2016-07-07 09:06:00

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

代码语言:javascript
复制
 getCurrentInputConnection().commitText("the spoken string here", 1);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21752155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档