带有SpeechRecognizer的Android自定义键盘 [英] Android Custom Keyboard with SpeechRecognizer

查看:128
本文介绍了带有SpeechRecognizer的Android自定义键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能齐全的自定义android键盘,在其中必须添加语音识别.这是我执行的相关部分

I have a fully functional custom android keyboard in which i have to add speech recognition. Here are the relevant parts of the implementation i have

public class CustomInputMethodService 
    extends InputMethodService
    implements <random stuff> {

    private SpeechRecognizer mSpeechRecognizer;
    private RecognitionListener mSpeechlistener;

    public void onCreate() {
        super.onCreate();
        mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
        mSpeechlistener = new CustomRecognitionListener();
        mSpeechRecognizer.setRecognitionListener(mSpeechlistener);
    }

    @Override
    public void onPress(int primaryCode) {
        if (primaryCode == KeyCodes.VOICE_INPUT) {
            mSpeechRecognizer.startListening(getSpeechIntent());
        }else if(..){
            ...
        }
    }

    private Intent getSpeechIntent() {
        Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
        speechIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, false);
        return speechIntent;
    }

}

CustomRecognitionListener的相关方法很简单:

The relevant method of the CustomRecognitionListener is simply:

        @Override
        public void onResults(Bundle results) {
            ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            Log.d(TAG, "onResults: ----> " + matches.get(0));
            if(matches != null && matches.size() > 0) {
                writeText(matches.get(0));
            }
        }

此代码可以正常工作.这里的问题是,我希望当用户点击麦克风键时,该行为与Google键盘上发生的行为类似:

This code is working just fine. The twist here is that i want a similar behaviour to what happens on google keyboard when the uset taps the microphone key:

理想情况下,这可以通过以下方式实现:

This would ideally by achieved by something like:

Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
try {
    startActivityForResult(voiceIntent, Constants.RESULT_SPEECH);
} catch (ActivityNotFoundException ex) {
    DebugLog.e(TAG, "Not found excpetion onKeyDown: " + ex);
}

但是,由于键侦听器已打开并且InputMethodService无法调用startActivityForResult. 达到此目的的理想方法是什么?我是否应该简单地开始没有布局的新活动并具有对inputMethodService的回调?看起来很乱

However, since the key listener is on and InputMethodService im not able to call startActivityForResult. What is the ideal way to accomplish this? Should i simply start a new activity without a layout and have a callback to the inputMethodService? seems messy

推荐答案

您的屏幕截图显示"Google语音输入",这是一个独立的IME,由Google键盘在按下其麦克风按钮时调用.因此,您的IME应该做同样的事情:用提供语音键入功能的IME代替自己,并希望在完成语音键入操作后有指向您IME的反向链接.

Your screenshot shows "Google voice typing", which is an independent IME that is called by the Google Keyboard when its microphone button is pressed. So, your IME should do the same: replace itself with an IME that provides voice typing, and hope that there is a backlink to your IME once the voice typing is done.

最简单的实现是在IME子类型之间进行切换,但您可能希望拥有更多控制权,例如使用特定的输入参数等启动特定的IME.我不确定实现此额外控制的最佳/标准方法是什么.

The simplest implementation would be Switching among IME Subtypes, but you might want to have more control, e.g. start a specific IME with specific input parameters, etc. I'm not sure what is the best/standard way to achieve this extra control.

以语音输入IME为例,您可以查看(我的应用程序)Kõnele.

For an example of a voice typing IME you could look into (my app) Kõnele.

这篇关于带有SpeechRecognizer的Android自定义键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆