TextToSpeech tts = new TextToSpeech(this, this); tts.setLanguage(Locale.US); tts.speak("Text to say aloud", TextToSpeech.QUEUE_ADD, null);
public static void stopTts() { if (mTts != null) { if (sTextQueue != null) { sTextQueue.clear(); } mTts.stop(); } } }
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);
@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); }
@Before public void setUp() throws Exception { activity = Robolectric.buildActivity(Activity.class).create().get(); listener = new TextToSpeech.OnInitListener() { @Override public void onInit(int i) { } }; mockListener = mock(UtteranceProgressListener.class); textToSpeech = new TextToSpeech(activity, listener); }
@Override public void onInit(final int status) { if (status == TextToSpeech.SUCCESS) { Log.d("OnInitListener", "Text to speech engine started successfully."); tts.setLanguage(Locale.US); } else { Log.d("OnInitListener", "Error starting the text to speech engine."); } } };
@Test public void isShutdown_shouldReturnTrueAfterShutdown() throws Exception { textToSpeech.shutdown(); assertThat(shadowOf(textToSpeech).isShutdown()).isTrue(); }
/** * Returns true if the TTS engine supports the language of the locale represented by localeCode * (which should be in the format returned by Locale.toString()), false otherwise. */ private static boolean isLanguageAvailable(String localeCode) { return mTts.isLanguageAvailable(localeFromStringIgnoringScriptAndExtensions(localeCode)) >= TextToSpeech.LANG_AVAILABLE; }
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);
@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 isStopped_shouldReturnTrueAfterStop() throws Exception { textToSpeech.stop(); assertThat(shadowOf(textToSpeech).isStopped()).isTrue(); }
/** * 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)); }
@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 @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 public void getLastSpokenText_shouldReturnSpokenText() throws Exception { textToSpeech.speak("Hello", TextToSpeech.QUEUE_FLUSH, null); assertThat(shadowOf(textToSpeech).getLastSpokenText()).isEqualTo("Hello"); }
@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 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(); }