How to add Continues Speech Recognition in my Android Application?

一曲冷凌霜 提交于 2019-12-07 20:54:18

问题


I am try to implement Continues Speech Recognition in my Android Application. I have followed this Link coding. this Continues Speech Recognition worked before two days. But now Speech Recognition not working good it will be taking more time for speech listening. how to resolve this problem. Please guide me. Thanks

Recognition coding:

// starts the service
    protected void startListening() {
        try {
            initSpeech();
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            //intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
             if (!intent.hasExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE))
            {
                intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
                        "com.dummy");
            }
            sr.startListening(intent);
        } catch(Exception ex) {
            Log.d("SpeechRecognitionService", "Bei der Initialisierung des SpeechRecognizers ist ein Fehler aufgetreten");
        }
    }

    // stops the service
    protected void stopListening() {
        if (sr != null) {
            sr.stopListening();
            sr.cancel();
            sr.destroy();
        }
        sr = null;
    }

    protected void initSpeech() {
        if (sr == null) {
            sr = SpeechRecognizer.createSpeechRecognizer(this);
            if (!SpeechRecognizer.isRecognitionAvailable(context)) {
                Toast.makeText(context, "Speech Recognition is not available",
                        Toast.LENGTH_LONG).show();
                finish();
            }
            sr.setRecognitionListener(VoiceRecognitionListener.getInstance());
        }
    }

User starts speaking

public void onBeginningOfSpeech() {
            System.out.println("Starting to listen");
        }

        public void onBufferReceived(byte[] buffer) { }

        // User finished speaking
        public void onEndOfSpeech() {
            System.out.println("Waiting for result...");
        }

回答1:


I found the solution to reducing the time between speaking and receiving the results.

Request PARTIAL RESULTS as these are delivered before the FULL RESULTS.

I used these EXTRAS:-

mRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, Locale.getDefault().getLanguage().trim());
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 100);

mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);

mSongSpeechRecognitionListener = new SongSpeechRecognitionListener(mRippleBackground, mFloatingActionButton);

mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(mSongSpeechRecognitionListener);

Then in partial results

public void onPartialResults(final Bundle partialResults) {
    Log.i(LOG_TAG, "onPartialResults()");

    final List<String> partialResultList = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

for (final String result : partialResultList) {

    if (result.isEmpty()) {
    } else {
        mPartialResults++;
        if (mPartialResults == mPartialResultsMAX) {
            Log.i(LOG_TAG, "onPartialResults() EXECUTE");
            mFloatingActionButton.setEnabled(true);
            mAsyncTask.execute(result);
                break;
            }
        }
    }
}

I set mPartialResultsMAX to 2 as it seemed the first partial result was only ever a single word

When you receive PARTIAL RESULTS you may want to cancel the speech recogniser.



来源:https://stackoverflow.com/questions/38179290/how-to-add-continues-speech-recognition-in-my-android-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!