Code example for TextToSpeech

Methods: setLanguagesetOnUtteranceProgressListenerspeak

4
	} 
 
	@Override 
	public void onInit(int status) {
		if (status == TextToSpeech.SUCCESS) {
			myTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
				@Override 
				public void onDone(String utteranceId) {
					/* 
					 * When myTTS is done speaking the current "audio file", 
					 * call playAudio on the next audio file. 
					 */ 
					if (utteranceId.equals("utteranceId")) {
						myTTS.stop();
						if (currentFile < filenames.size() - 1) {
							currentFile++;
							playAudio(filenames.get(currentFile));
						} else { 
							filenames.clear();
							currentFile = 0;
						} 
					} 
				} 
 
				@Override 
				public void onError(String utteranceId) {
				} 
 
				@Override 
				public void onStart(String utteranceId) {
				} 
			}); 
 
			// Set up myTTS and speak the prompt text aloud 
			myTTS.setLanguage(Locale.US);
			speakOut(prompt);
		} else { 
			Log.e("TTS", "Initilization Failed!");
		} 
	} 
 
	/** 
	 * Speaks text aloud using Text To Speech. 
	 *  
	 * @param text 
	 *            the text 
	 */ 
	public void speakOut(String text) {
		myTTS.speak(text, TextToSpeech.QUEUE_ADD, params);
	} 
 
	/** 
	 * Adds a resourceId to filenames 
	 *  
Experience pair programming with AI  Get Codota for Java