Example usage for android.speech.tts TextToSpeech QUEUE_ADD

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

Introduction

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

Prototype

int QUEUE_ADD

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

Click Source Link

Document

Queue mode where the new entry is added at the end of the playback queue.

Usage

From source file:com.ct.speech.TTS.java

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

    try {//  w w  w  .  j a v a  2  s .  co  m
        if (action.equals("speak")) {
            String text = args.getString(0);
            if (isReady()) {
                mTts.speak(text, TextToSpeech.QUEUE_ADD, null);
                return new PluginResult(status, result);
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                return new PluginResult(PluginResult.Status.ERROR, error);
            }
        } else if (action.equals("silence")) {
            if (isReady()) {
                mTts.playSilence(args.getLong(0), TextToSpeech.QUEUE_ADD, null);
                return new PluginResult(status, result);
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                return new PluginResult(PluginResult.Status.ERROR, error);
            }
        } else if (action.equals("startup")) {
            if (mTts == null) {
                this.startupCallbackId = callbackId;
                state = TTS.INITIALIZING;
                mTts = new TextToSpeech((Context) ctx, this);
                //mTts.setLanguage(Locale.US);         
            }
            PluginResult pluginResult = new PluginResult(status, TTS.INITIALIZING);
            pluginResult.setKeepCallback(true);
            return pluginResult;
        } else if (action.equals("shutdown")) {
            if (mTts != null) {
                mTts.shutdown();
            }
            return new PluginResult(status, result);
        } else if (action.equals("stop")) {
            if (mTts != null) {
                mTts.stop();
            }
            return new PluginResult(status, result);
        } else if (action.equals("getLanguage")) {
            if (mTts != null) {
                result = mTts.getLanguage().toString();
                return 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";
                return 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";
                return new PluginResult(status, result);
            }
        }
        return new PluginResult(status, result);
    } catch (JSONException e) {
        e.printStackTrace();
        return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
    }
}

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 {/*www. ja  v  a 2 s  .  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);
            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 ww.  ja va 2s . co  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/*  ww w  . j a  v  a 2  s  .com*/
 *            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.guanqing.hao.OcrCaptureActivity.java

/**
 * onTap is called to speak the tapped TextBlock, if any, out loud.
 *
 * @param rawX - the raw position of the tap
 * @param rawY - the raw position of the tap.
 * @return true if the tap was on a TextBlock
 *//*  w w  w  .j  a  v a  2s.  com*/
private boolean onTap(float rawX, float rawY) {
    OcrGraphic graphic = mGraphicOverlay.getGraphicAtLocation(rawX, rawY);
    TextBlock text = null;
    if (graphic != null) {
        text = graphic.getTextBlock();
        if (text != null && text.getValue() != null) {
            // Speak the string.
            tts.speak(text.getValue(), TextToSpeech.QUEUE_ADD, null, "DEFAULT");
        }
    }
    return text != null;
}

From source file:org.envirocar.app.application.service.BackgroundServiceImpl.java

private void doTextToSpeech(String string) {
    if (ttsAvailable) {
        tts.speak("enviro car ".concat(string), TextToSpeech.QUEUE_ADD, null);
    }
}

From source file:com.mobile.lapa.waitandsee.MainActivity.java

private void speakText(String text) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        textToSpeech.speak(text, TextToSpeech.QUEUE_ADD, null, null);
    } else {/*from w w  w  .ja  va 2 s .co m*/
        textToSpeech.speak(text, TextToSpeech.QUEUE_ADD, null);
    }
}

From source file:com.annuletconsulting.homecommand.node.AsyncSend.java

/**
 * This launched an asynchronous process that speaks the text provided if it isn't null or 0 length
 * //w w w . j  ava2 s.c o  m
 * TODO: run the finishedListener after speech is completed rather than after this is launched.
 * 
 * @param text
 * @return
 */
public boolean speak(String text) {
    if (text != null && text.length() > 0) {
        tts.speak(text, TextToSpeech.QUEUE_ADD, null);
        return true;
    }
    return false;
}

From source file:com.perchtech.humraz.blind.OcrCaptureActivity.java

private boolean onTap(float rawX, float rawY) {
    OcrGraphic graphic = mGraphicOverlay.getGraphicAtLocation(rawX, rawY);
    TextBlock text = null;/*from   w  w w. jav  a 2 s.  com*/
    if (graphic != null) {
        text = graphic.getTextBlock();
        if (text != null && text.getValue() != null) {
            Log.d(TAG, "text data is being spoken! " + text.getValue());
            // Speak the string.
            tts.speak(text.getValue(), TextToSpeech.QUEUE_ADD, null, "DEFAULT");
        } else {
            Log.d(TAG, "text data is null");
        }
    } else {
        Log.d(TAG, "no text detected");
    }
    return text != null;
}

From source file:com.abid_mujtaba.fetchheaders.MainActivity.java

private void speak(Email email) // Method for applying TextToSpeech to an email
{
    if (mTTS != null) // mTTS is set to null if the initialization fails
    {/*from www.  j  a  va  2 s.c o m*/
        // NOTE: Adding periods inside the string introduces delays in the Speech synthesized from the text
        String msg = String.format("From %s. %s.", email.from(), email.subject());

        mTTS.speak(msg, TextToSpeech.QUEUE_ADD, null);
    }
}