Example usage for android.speech RecognizerIntent EXTRA_PREFER_OFFLINE

List of usage examples for android.speech RecognizerIntent EXTRA_PREFER_OFFLINE

Introduction

In this page you can find the example usage for android.speech RecognizerIntent EXTRA_PREFER_OFFLINE.

Prototype

String EXTRA_PREFER_OFFLINE

To view the source code for android.speech RecognizerIntent EXTRA_PREFER_OFFLINE.

Click Source Link

Document

Optional boolean, to be used with #ACTION_RECOGNIZE_SPEECH , #ACTION_VOICE_SEARCH_HANDS_FREE , #ACTION_WEB_SEARCH to indicate whether to only use an offline speech recognition engine.

Usage

From source file:org.botlibre.sdk.activity.MicConfiguration.java

@TargetApi(23)
private void beginListening() {
    setStreamVolume();//from w  w w .  j a  v a  2 s . c o  m
    lastReply = System.currentTimeMillis();

    muteMicBeep(true);

    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    if (MainActivity.offlineSpeech) {
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, MainActivity.voice.language);

        if (!this.failedOfflineLanguage) {
            //en-US will use the English in offline.
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
            // intent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true);
        }
        intent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true);
    } else {
        if (MainActivity.voice.language != null && !MainActivity.voice.language.isEmpty()) {
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, MainActivity.voice.language);
            if (!this.failedOfflineLanguage) {
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, MainActivity.voice.language);
            }
        } else {
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en");
            if (!this.failedOfflineLanguage) {
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en");
            }
        }
    }

    try {
        Log.d("BeginListening", "StartListening");
        this.speech.startListening(intent);
        setMicIcon(true, false);
    } catch (ActivityNotFoundException a) {
        Log.d("BeginListening", "CatchError: " + a.getMessage());
        Toast t = Toast.makeText(getApplicationContext(), "Your device doesn't support Speech to Text",
                Toast.LENGTH_SHORT);
        t.show();
        txt.setText("Status: Your device doesn't support Speech to text.");
    }
}

From source file:org.botlibre.sdk.activity.ChatActivity.java

@TargetApi(23)
private void beginListening() {
    lastReply = System.currentTimeMillis();
    setStreamVolume();/*  ww  w  . j  a va  2 s.co  m*/
    debug("beginListening:");

    try {
        if (!MainActivity.handsFreeSpeech) {
            return;
        }
        if (MainActivity.handsFreeSpeech) {
            muteMicBeep(true);
            isListening = true;
        }

        if (!MainActivity.listenInBackground) {
            muteMicBeep(false);
            return;
        }

    } catch (Exception ignore) {
        Log.e("Error", "BeginListening");
    }

    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    if (MainActivity.offlineSpeech) {
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, MainActivity.voice.language);

        if (!this.failedOfflineLanguage) {
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
            // intent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true);
        }
        intent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true);
    } else {
        if (MainActivity.voice.language != null && !MainActivity.voice.language.isEmpty()) {
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, MainActivity.voice.language);
            if (!this.failedOfflineLanguage) {
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, MainActivity.voice.language);
            }
        } else {
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en");
            if (!this.failedOfflineLanguage) {
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en");
            }
        }
    }

    try {
        Log.d("BeginListening", "StartListening");
        this.speech.startListening(intent);
        setMicIcon(true, false);
    } catch (ActivityNotFoundException a) {
        Log.d("BeginListening", "CatchError: " + a.getMessage());
        Toast t = Toast.makeText(getApplicationContext(), "Your device doesn't support Speech to Text",
                Toast.LENGTH_SHORT);
        t.show();
    }
}