TextToSpeech tts= new TextToSpeech(context, new OnInitListener() { @Override public void onInit(int status) { mTts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() { @Override public void onUtteranceCompleted(String utteranceId) { runOnUiThread(new Runnable() { @Override public void run() { //UI changes } }); } }); } }); HashMap<String, String> params = new HashMap<String, String>(); params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"stringId"); tts.speak("Text to Speak",TextToSpeech.QUEUE_FLUSH, params);
TextToSpeech tts= new TextToSpeech(context, new OnInitListener() { @Override public void onInit(int status) { mTts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() { @Override public void onUtteranceCompleted(String utteranceId) { runOnUiThread(new Runnable() { @Override public void run() { //UI changes } }); } }); } }); HashMap<String, String> params = new HashMap<String, String>(); params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"stringId"); tts.speak("Text to Speak",TextToSpeech.QUEUE_FLUSH, params);
public class TTSActivity extends Activity implements OnInitListener, OnUtteranceCompletedListener, ... { private TextToSpeech mTts; ........... private void speak(String text) { if(text != null) { HashMap<String, String> myHashAlarm = new HashMap<String, String>(); myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM)); myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "SOME MESSAGE"); mTts.speak(text, TextToSpeech.QUEUE_FLUSH, myHashAlarm); } } // Fired after TTS initialization public void onInit(int status) { if(status == TextToSpeech.SUCCESS) { mTts.setOnUtteranceCompletedListener(this); } } // It's callback public void onUtteranceCompleted(String utteranceId) { Log.i(TAG, utteranceId); //utteranceId == "SOME MESSAGE" } ........... }
TextToSpeech tts = new TextToSpeech(this, this); tts.setLanguage(Locale.US); tts.speak("Text to say aloud", TextToSpeech.QUEUE_ADD, null);
/** * Speaks the string using the specified queuing strategy and speech parameters. * * @param params The real implementation converts the hashmap into a bundle, but the bundle * argument is not used in the shadow implementation. */ @Implementation protected int speak( final String text, final int queueMode, final HashMap<String, String> params) { if (RuntimeEnvironment.getApiLevel() >= LOLLIPOP) { return Shadow.directlyOn(tts, TextToSpeech.class).speak(text, queueMode, params); } return speak( text, queueMode, null, params == null ? null : params.get(Engine.KEY_PARAM_UTTERANCE_ID)); }
@SuppressWarnings("deprecation") // Movement to new API tracked in github as #5021 public static void speak(String text, String loc, int queueMode) { int result = mTts.setLanguage(localeFromStringIgnoringScriptAndExtensions(loc)); if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { Toast.makeText(mReviewer.get(), mReviewer.get().getString(R.string.no_tts_available_message) +" ("+loc+")", Toast.LENGTH_LONG).show(); Timber.e("Error loading locale " + loc); } else { if (mTts.isSpeaking() && queueMode == TextToSpeech.QUEUE_FLUSH) { Timber.d("tts engine appears to be busy... clearing queue"); stopTts(); //sTextQueue.add(new String[] { text, loc }); } Timber.d("tts text '%s' to be played for locale (%s)",text, loc); mTts.speak(mTextToSpeak, queueMode, mTtsParams); } }
/** * 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 */ private boolean onTap(float rawX, float rawY) { OcrGraphic graphic = graphicOverlay.getGraphicAtLocation(rawX, rawY); TextBlock text = null; 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; }
@Test public void threeArgumentSpeak_withUtteranceId_shouldGetCallbackUtteranceId() throws Exception { textToSpeech.setOnUtteranceProgressListener(mockListener); HashMap<String, String> paramsMap = new HashMap<>(); paramsMap.put(Engine.KEY_PARAM_UTTERANCE_ID, "ThreeArgument"); textToSpeech.speak("Hello", TextToSpeech.QUEUE_FLUSH, paramsMap); Robolectric.flushForegroundThreadScheduler(); verify(mockListener).onStart("ThreeArgument"); verify(mockListener).onDone("ThreeArgument"); }
@Test public void threeArgumentSpeak_withoutUtteranceId_shouldDoesNotGetCallback() throws Exception { textToSpeech.setOnUtteranceProgressListener(mockListener); textToSpeech.speak("Hello", TextToSpeech.QUEUE_FLUSH, null); Robolectric.flushForegroundThreadScheduler(); verify(mockListener, never()).onStart(null); verify(mockListener, never()).onDone(null); }
@Test @Config(minSdk = LOLLIPOP) public void speak_withUtteranceId_shouldReturnSpokenText() throws Exception { textToSpeech.speak("Hello", TextToSpeech.QUEUE_FLUSH, null, "TTSEnable"); assertThat(shadowOf(textToSpeech).getLastSpokenText()).isEqualTo("Hello"); }
@Test @Config(minSdk = LOLLIPOP) public void onUtteranceProgressListener_shouldGetCallbackUtteranceId() throws Exception { textToSpeech.setOnUtteranceProgressListener(mockListener); textToSpeech.speak("Hello", TextToSpeech.QUEUE_FLUSH, null, "TTSEnable"); Robolectric.flushForegroundThreadScheduler(); verify(mockListener).onStart("TTSEnable"); verify(mockListener).onDone("TTSEnable"); } }
@Test public void getQueueMode_shouldReturnMostRecentQueueMode() throws Exception { textToSpeech.speak("Hello", TextToSpeech.QUEUE_ADD, null); assertThat(shadowOf(textToSpeech).getQueueMode()).isEqualTo(TextToSpeech.QUEUE_ADD); }
@Test public void getLastSpokenText_shouldReturnSpokenText() throws Exception { textToSpeech.speak("Hello", TextToSpeech.QUEUE_FLUSH, null); assertThat(shadowOf(textToSpeech).getLastSpokenText()).isEqualTo("Hello"); }
@Test public void isStopped_shouldReturnFalseAfterSpeak() throws Exception { textToSpeech.speak("Hello", TextToSpeech.QUEUE_FLUSH, null); assertThat(shadowOf(textToSpeech).isStopped()).isFalse(); }
@Test public void clearLastSpokenText_shouldSetLastSpokenTextToNull() throws Exception { textToSpeech.speak("Hello", TextToSpeech.QUEUE_FLUSH, null); shadowOf(textToSpeech).clearLastSpokenText(); assertThat(shadowOf(textToSpeech).getLastSpokenText()).isNull(); }
public void speakAudio(String sentence) { if (mediaType == MediaType.TTS) { HashMap<String, String> params = new HashMap<>(); params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "stringId"); mTextToSpeech.speak(sentence, TextToSpeech.QUEUE_FLUSH, params); } }
int result = mTts.setLanguage(Locale.US); if (result != TextToSpeech.LANG_MISSING_DATA && result != TextToSpeech.LANG_NOT_SUPPORTED) { mTts.speak(spokenText, TextToSpeech.QUEUE_FLUSH, null);
mTts.speak(str, TextToSpeech.QUEUE_FLUSH, null);
private void sayHello() { // Select a random hello. int helloLength = HELLOS.length; String hello = HELLOS[RANDOM.nextInt(helloLength)]; mTts.speak(hello, TextToSpeech.QUEUE_FLUSH, // Drop all pending entries in the playback queue. null); }