Code example for TextToSpeech

Methods: isSpeaking, setOnUtteranceCompletedListener, setPitch, setSpeechRate, speak, stop

0
	 
	@Kroll.method 
	public boolean speak(String text, @Kroll.argument(optional = true) String utteranceId) {
		Log.d(LCAT, "speak " + text);
		if (initialized) {
			if (0 < text.length()) {
				if (tts.setPitch(getPitch()) == TextToSpeech.ERROR) {
					Log.e("TTS", "Ptich(" + getPitch() + ") set error.");
				} 
				if (tts.setSpeechRate(getRate()) == TextToSpeech.ERROR) {
					Log.e("TTS", "Speech rate(" + getRate() + ") set error.");
				} 
				stopSpeaking(); 
				tts.setOnUtteranceCompletedListener(this);
				HashMap<String, String> options = new HashMap<String, String>();
				//myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, 
				 //       String.valueOf(AudioManager.STREAM_ALARM)); 
				options.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,
						utteranceId);
				tts.speak(text, TextToSpeech.QUEUE_FLUSH, options);
			} 
		} 
		return true; 
	} 
	// implements OnUtteranceCompletedListener 
	@Override 
	public void onUtteranceCompleted(String utteranceId) {
		KrollDict data = new KrollDict();
		data.put(TiC.EVENT_PROPERTY_SOURCE, TtsModule.this);
		data.put(TTS_UTTERANCE_ID, utteranceId);
		fireEvent(TTS_UTTERANCE_COMPLETE, data);
	} 
 
	@Kroll.method 
	public void stopSpeaking() { 
		Log.d(LCAT, "stopSpeaking ");
		if (initialized) {
			if (isSpeaking()) { 
				tts.stop();
			} 
		} 
	} 
 
//	@Kroll.method 
	@Kroll.getProperty 
	public boolean getIsInitialized() { 
		return initialized;
	} 
 
	@Kroll.method 
	@Kroll.setProperty 
	public void setPitch(float pitch) {
		this.pitch = pitch;