Kaljurand / K6nele

An Android app that offers speech-to-text user interfaces to other apps
http://kaljurand.github.io/K6nele/
Apache License 2.0
267 stars 83 forks source link

How to have a non-stop Speech to Text? #71

Closed leekinon closed 4 years ago

leekinon commented 4 years ago

I success to use following code to create speech to text feature. however, The MICButtom will stop record everytime when I stop speaking. How can I do non-stop record?

` private SpeechInputView.SpeechInputViewListener getSpeechInputViewListener() { return new AbstractSpeechInputViewListener() {

        private Iterable<UtteranceRewriter> mRewriters;

        @Override
        public void onComboChange(String language, ComponentName service) {
            mRewriters = Utils.genRewriters(mPrefs, mRes, new String[]{"Base", "Commands"}, language, service, getComponentName());
        }

        @Override
        public void onFinalResult(List<String> results, Bundle bundle) {
            if (!results.isEmpty()) {
                String result = results.get(0);
                getResult(result);
                // TODO: store the JSON also in the list, so that it can be reexecuted later
                IntentUtils.launchIfIntent(MainActivity.this, mRewriters, result);
            }
        }

        @Override
        public void onBufferReceived(byte[] buffer) {
            // TODO
        }

        @Override
        public void onError(int errorCode) {
            if (errorCode == SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS) {
                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{Manifest.permission.RECORD_AUDIO},
                        PERMISSION_REQUEST_RECORD_AUDIO);
            } else {
                setResultError(errorCode);
            }
        }

        protected void setResultError(int errorCode) {
            if (mIsReturnErrors) {
                Integer errorCodeIntent = mErrorCodesServiceToIntent.get(errorCode);
                setResult(errorCodeIntent);
                finish();
            }
        }

        @Override
        public void onStartListening() {
            closeKeyboard();
            if (!isNetworkAvailable()) {
                editText.setText("沒有網路");
            }
        }
    };
}`
Kaljurand commented 4 years ago

Sorry about the delayed response. Did you find a solution to the issue?

In general, the recognition service decides when the recording/recognition ends (after a pause, or after e.g. 30 sec.), and you cannot control or override this at the level of SpeechInputView. You can try however to restart the recognition after receiving onFinalResult. This would require a change to the source code, i.e. there is no setting or EXTRA for that.