Example usage for android.speech.tts TextToSpeech QUEUE_FLUSH

List of usage examples for android.speech.tts TextToSpeech QUEUE_FLUSH

Introduction

In this page you can find the example usage for android.speech.tts TextToSpeech QUEUE_FLUSH.

Prototype

int QUEUE_FLUSH

To view the source code for android.speech.tts TextToSpeech QUEUE_FLUSH.

Click Source Link

Document

Queue mode where all entries in the playback queue (media to be played and text to be synthesized) are dropped and replaced by the new entry.

Usage

From source file:com.hichinaschool.flashcards.anki.ReadText.java

public static void speak(String text, String loc) {
    int result = mTts.setLanguage(new Locale(loc));
    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
        Log.e(AnkiDroidApp.TAG, "Error loading locale " + loc.toString());
    } else {/* w  ww .ja va 2s  .co m*/
        if (mTts.isSpeaking()) {
            sTextQueue.add(new String[] { text, loc });
        } else {
            mTts.speak(mTextToSpeak, TextToSpeech.QUEUE_FLUSH, mTtsParams);
        }
    }
}

From source file:com.vaporwarecorp.mirror.feature.texttospeech.TextToSpeechManagerImpl.java

@Override
public void speak(String textToSpeak) {
    if (mTextToSpeech == null || isEmpty(textToSpeak)) {
        return;/* w ww.j  a va2  s  .  c  o m*/
    }
    mTextToSpeech.speak(textToSpeak, TextToSpeech.QUEUE_FLUSH, null, String.valueOf(hashCode()));
}

From source file:info.jmfavreau.bifrostgears.BifrostGears.java

private void DoIt(View v) {
    HSLColorPicker cp = (HSLColorPicker) getFragmentManager().findFragmentById(R.id.hsl_color_picker);
    SemanticColor c = cp.getSemanticColor();
    if (Build.VERSION.RELEASE.startsWith("5")) {
        textToSpeech.speak(c.toString(), TextToSpeech.QUEUE_FLUSH, null, null);
    } else {/* w ww .ja v a2 s . c o m*/
        textToSpeech.speak(c.toString(), TextToSpeech.QUEUE_FLUSH, null);
    }
}

From source file:com.wordsbaking.cordova.tts.TTS.java

@Override
public void onInit(int status) {
    if (status != TextToSpeech.SUCCESS) {
        tts = null;//from  w ww  .j  a  va 2s. c  o m
    } else {
        // warm up the tts engine with an empty string
        HashMap<String, String> ttsParams = new HashMap<String, String>();
        ttsParams.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "");
        tts.setLanguage(new Locale("en", "US"));
        tts.speak("", TextToSpeech.QUEUE_FLUSH, ttsParams);

        ttsInitialized = true;
    }
}

From source file:com.phonegap.plugins.speech.TTS.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
    PluginResult.Status status = PluginResult.Status.OK;
    String result = "";
    this.callbackContext = callbackContext;

    try {//from   w  w w.  j  a  v a2 s  . c om
        if (action.equals("speak")) {
            String text = args.getString(0);
            if (isReady()) {
                HashMap<String, String> map = null;
                map = new HashMap<String, String>();
                map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, callbackContext.getCallbackId());
                mTts.speak(text, TextToSpeech.QUEUE_ADD, map);
                PluginResult pr = new PluginResult(PluginResult.Status.NO_RESULT);
                pr.setKeepCallback(true);
                callbackContext.sendPluginResult(pr);
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("interrupt")) {
            String text = args.getString(0);
            if (isReady()) {
                HashMap<String, String> map = null;
                map = new HashMap<String, String>();
                //map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, callbackId);
                mTts.speak(text, TextToSpeech.QUEUE_FLUSH, map);
                PluginResult pr = new PluginResult(PluginResult.Status.NO_RESULT);
                pr.setKeepCallback(true);
                callbackContext.sendPluginResult(pr);
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("stop")) {
            if (isReady()) {
                mTts.stop();
                callbackContext.sendPluginResult(new PluginResult(status, result));
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("silence")) {
            if (isReady()) {
                HashMap<String, String> map = null;
                map = new HashMap<String, String>();
                map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, callbackContext.getCallbackId());
                mTts.playSilence(args.getLong(0), TextToSpeech.QUEUE_ADD, map);
                PluginResult pr = new PluginResult(PluginResult.Status.NO_RESULT);
                pr.setKeepCallback(true);
                callbackContext.sendPluginResult(pr);
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("speed")) {
            if (isReady()) {
                float speed = (float) (args.optLong(0, 100)) / (float) 100.0;
                mTts.setSpeechRate(speed);
                callbackContext.sendPluginResult(new PluginResult(status, result));
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("pitch")) {
            if (isReady()) {
                float pitch = (float) (args.optLong(0, 100)) / (float) 100.0;
                mTts.setPitch(pitch);
                callbackContext.sendPluginResult(new PluginResult(status, result));
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("startup")) {
            this.startupCallbackContext = callbackContext;
            if (mTts == null) {
                state = TTS.INITIALIZING;
                mTts = new TextToSpeech(cordova.getActivity().getApplicationContext(), this);
            }
            PluginResult pluginResult = new PluginResult(status, TTS.INITIALIZING);
            pluginResult.setKeepCallback(true);
            startupCallbackContext.sendPluginResult(pluginResult);
        } else if (action.equals("shutdown")) {
            if (mTts != null) {
                mTts.shutdown();
            }
            callbackContext.sendPluginResult(new PluginResult(status, result));
        } else if (action.equals("getLanguage")) {
            if (mTts != null) {
                result = mTts.getLanguage().toString();
                callbackContext.sendPluginResult(new PluginResult(status, result));
            }
        } else if (action.equals("isLanguageAvailable")) {
            if (mTts != null) {
                Locale loc = new Locale(args.getString(0));
                int available = mTts.isLanguageAvailable(loc);
                result = (available < 0) ? "false" : "true";
                callbackContext.sendPluginResult(new PluginResult(status, result));
            }
        } else if (action.equals("setLanguage")) {
            if (mTts != null) {
                Locale loc = new Locale(args.getString(0));
                int available = mTts.setLanguage(loc);
                result = (available < 0) ? "false" : "true";
                callbackContext.sendPluginResult(new PluginResult(status, result));
            }
        }
        return true;
    } catch (JSONException e) {
        e.printStackTrace();
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
    }
    return false;
}

From source file:com.firerunner.cordova.TTS.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
    PluginResult.Status status = PluginResult.Status.OK;
    String result = "";
    this.callbackContext = callbackContext;

    try {/*w w w.j  ava2s.c  o  m*/
        if (action.equals("speak")) {
            String text = args.getString(0);
            if (isReady()) {
                HashMap<String, String> map = null;
                map = new HashMap<String, String>();
                map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, callbackContext.getCallbackId());
                mTts.speak(text, TextToSpeech.QUEUE_ADD, map);
                PluginResult pr = new PluginResult(PluginResult.Status.NO_RESULT);
                pr.setKeepCallback(true);
                callbackContext.sendPluginResult(pr);
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("interrupt")) {
            String text = args.getString(0);
            if (isReady()) {
                HashMap<String, String> map = null;
                map = new HashMap<String, String>();
                //map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, callbackId);
                mTts.speak(text, TextToSpeech.QUEUE_FLUSH, map);
                PluginResult pr = new PluginResult(PluginResult.Status.NO_RESULT);
                pr.setKeepCallback(true);
                callbackContext.sendPluginResult(pr);
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("stop")) {
            if (isReady()) {
                mTts.stop();
                callbackContext.sendPluginResult(new PluginResult(status, result));
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("silence")) {
            if (isReady()) {
                HashMap<String, String> map = null;
                map = new HashMap<String, String>();
                map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, callbackContext.getCallbackId());
                mTts.playSilence(args.getLong(0), TextToSpeech.QUEUE_ADD, map);
                PluginResult pr = new PluginResult(PluginResult.Status.NO_RESULT);
                pr.setKeepCallback(true);
                callbackContext.sendPluginResult(pr);
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("speed")) {
            if (isReady()) {
                float speed = (float) (args.optLong(0, 100)) / (float) 100.0;
                mTts.setSpeechRate(speed);
                callbackContext.sendPluginResult(new PluginResult(status, result));
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("pitch")) {
            if (isReady()) {
                float pitch = (float) (args.optLong(0, 100)) / (float) 100.0;
                mTts.setPitch(pitch);
                callbackContext.sendPluginResult(new PluginResult(status, result));
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("startup")) {

            this.startupCallbackContext = callbackContext;
            if (mTts == null) {
                state = TTS.INITIALIZING;
                mTts = new TextToSpeech(cordova.getActivity().getApplicationContext(), this);
                PluginResult pluginResult = new PluginResult(status, TTS.INITIALIZING);
                pluginResult.setKeepCallback(true);
                // do not send this as onInit is more reliable: domaemon
                // startupCallbackContext.sendPluginResult(pluginResult);
            } else {
                PluginResult pluginResult = new PluginResult(status, TTS.INITIALIZING);
                pluginResult.setKeepCallback(true);
                startupCallbackContext.sendPluginResult(pluginResult);
            }
        } else if (action.equals("shutdown")) {
            if (mTts != null) {
                mTts.shutdown();
                mTts = null;
            }
            callbackContext.sendPluginResult(new PluginResult(status, result));
        } else if (action.equals("getLanguage")) {
            if (mTts != null) {
                result = mTts.getLanguage().toString();
                callbackContext.sendPluginResult(new PluginResult(status, result));
            }
        } else if (action.equals("isLanguageAvailable")) {
            if (mTts != null) {
                Locale loc = new Locale(args.getString(0));
                int available = mTts.isLanguageAvailable(loc);
                result = (available < 0) ? "false" : "true";
                callbackContext.sendPluginResult(new PluginResult(status, result));
            }
        } else if (action.equals("setLanguage")) {
            if (mTts != null) {
                Locale loc = new Locale(args.getString(0));
                int available = mTts.setLanguage(loc);
                result = (available < 0) ? "false" : "true";
                callbackContext.sendPluginResult(new PluginResult(status, result));
            }
        }
        return true;
    } catch (JSONException e) {
        e.printStackTrace();
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
    }
    return false;
}

From source file:org.apache.cordova.plugins.speech.TTS.java

/**
 * Executes the request and returns PluginResult.
 *
 * @param action//from w w w  .  ja va  2  s .  co m
 *            The action to execute.
 * @param args
 *            JSONArry of arguments for the plugin.
 * @param callbackContext
 *            The callback context used when calling back into JavaScript.
 * @return True if the action was valid, false otherwise.
 */
@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext)
        throws JSONException {
    // Dispatcher
    PluginResult.Status status = PluginResult.Status.OK;
    String result = "";

    try {
        if (action.equals("speak")) {
            String text = args.getString(0);
            if (isReady()) {
                Log.d(LOG_TAG, "Speak " + text + ".");
                HashMap<String, String> map = null;
                map = new HashMap<String, String>();
                map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, callbackContext.getCallbackId());
                mTts.speak(text, TextToSpeech.QUEUE_ADD, map);
                PluginResult pr = new PluginResult(PluginResult.Status.NO_RESULT);
                pr.setKeepCallback(true);
                callbackContext.sendPluginResult(pr);
            } else {
                Log.d(LOG_TAG, "TTS service is still initialzing.");
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("interrupt")) {
            String text = args.getString(0);
            if (isReady()) {
                HashMap<String, String> map = null;
                map = new HashMap<String, String>();
                map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, callbackContext.getCallbackId());
                mTts.speak(text, TextToSpeech.QUEUE_FLUSH, map);
                PluginResult pr = new PluginResult(PluginResult.Status.NO_RESULT);
                pr.setKeepCallback(true);
                callbackContext.sendPluginResult(pr);
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("stop")) {
            if (isReady()) {
                mTts.stop();
                callbackContext.sendPluginResult(new PluginResult(status, result));
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("silence")) {
            if (isReady()) {
                mTts.playSilence(args.getLong(0), TextToSpeech.QUEUE_ADD, null);
                callbackContext.sendPluginResult(new PluginResult(status, result));
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("speed")) {
            if (isReady()) {
                float speed = (float) (args.optLong(0, 100)) / (float) 100.0;
                mTts.setSpeechRate(speed);
                callbackContext.sendPluginResult(new PluginResult(status, result));
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("pitch")) {
            if (isReady()) {
                float pitch = (float) (args.optLong(0, 100)) / (float) 100.0;
                mTts.setPitch(pitch);
                callbackContext.sendPluginResult(new PluginResult(status, result));
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("startup")) {
            if (mTts == null) {
                this.callbackContext = callbackContext;
                state = TTS.INITIALIZING;
                mTts = new TextToSpeech(cordova.getActivity().getApplicationContext(), this);
            }
            PluginResult pluginResult = new PluginResult(status, TTS.INITIALIZING);
            pluginResult.setKeepCallback(true);
            callbackContext.sendPluginResult(pluginResult);
        } else if (action.equals("shutdown")) {
            if (mTts != null) {
                mTts.shutdown();
            }
            callbackContext.sendPluginResult(new PluginResult(status, result));
        } else if (action.equals("getLanguage")) {
            if (mTts != null) {
                result = mTts.getLanguage().toString();
                callbackContext.sendPluginResult(new PluginResult(status, result));
            }
        } else if (action.equals("isLanguageAvailable")) {
            if (mTts != null) {
                Locale loc = new Locale(args.getString(0));
                int available = mTts.isLanguageAvailable(loc);
                result = (available < 0) ? "false" : "true";
                callbackContext.sendPluginResult(new PluginResult(status, result));
            }
        } else if (action.equals("setLanguage")) {
            if (mTts != null) {
                Locale loc = new Locale(args.getString(0));
                int available = mTts.setLanguage(loc);
                result = (available < 0) ? "false" : "true";
                callbackContext.sendPluginResult(new PluginResult(status, result));
            }
        } else {
            String res = "Unknown action: " + action;
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.INVALID_ACTION, res));
            return false;
        }
    } catch (JSONException e) {
        e.printStackTrace();
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
    }

    return true;
}

From source file:com.rstar.mobile.simpledemos.InvisibleFragment.java

private void startSpeaking() {
    if (mTextToSpeech != null) {
        // New versions (21 or above)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mTextToSpeech.speak(mMessage, TextToSpeech.QUEUE_FLUSH, null, messageId);
        }/*  www . j a  va 2 s .  com*/
        // Old versions (below 21)
        else {
            mTextToSpeech.speak(mMessage, TextToSpeech.QUEUE_FLUSH, null);
        }
    }
}

From source file:com.mobilevangelist.glass.helloworld.GetTheWeatherActivity.java

/**
 * Handle the tap event from the touchpad.
 *///  w  ww  .  jav  a 2 s  . c  om
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
    // Handle tap events.
    case KeyEvent.KEYCODE_DPAD_CENTER:
        //case KeyEvent.KEYCODE_ENTER:

        AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        audio.playSoundEffect(Sounds.TAP);

        _speech.speak("The weather is" + mCurrent + "Fahrenheit and" + mDescription, TextToSpeech.QUEUE_FLUSH,
                null);

        Bitmap bitmap = loadImageFromURL("http://openweathermap.org/img/w/10d.png");
        _weatherIconImageView.setImageBitmap(bitmap);

        return true;
    default:
        return super.onKeyDown(keyCode, event);
    }
}

From source file:com.rsamadhan.DomainListFragment.java

@TargetApi(21)
private void speakOut() {
    if (Build.VERSION.SDK_INT >= 21) {
        String text = getString(R.string.select_below_txt);
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
    }/*from ww  w. ja  va2s .c o m*/
}