Code example for TextToSpeech

Methods: isLanguageAvailable, setLanguage, setPitch, setSpeechRate, sleep, speak

0
			// Display an hint to the user about what he should say. 
			if (prompt == 0){
				try { 
					myText1 = "Are you looking for a room number, " +
							  "a professor, or a non-academic room?"; 
					myTextToSpeech.speak(myText1, TextToSpeech.QUEUE_FLUSH, null);
					Thread.currentThread();
					Thread.sleep(5000);
					intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Are you looking for a room number, " +
							  "a professor, or a non-academic room?"); 
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block 
					e.printStackTrace();
				} 
			} 
			else if (prompt == 1){
				try { 
					myText1 = "What is the room number you are looking for?";
					myTextToSpeech.speak(myText1, TextToSpeech.QUEUE_FLUSH, null);
					Thread.currentThread();
					Thread.sleep(3000);
					intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "What is the room number you are looking for?");
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block 
					e.printStackTrace();
				} 
 
			} 
			else if (prompt == 2){
				try { 
					myText1 = "Who is the professor you are looking for?";
					myTextToSpeech.speak(myText1, TextToSpeech.QUEUE_FLUSH, null);
					Thread.currentThread();
					Thread.sleep(3000);
					intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Who is the professor you are looking for?");
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block 
					e.printStackTrace();
				} 
 
			} 
			else if (prompt == 3){
				try { 
					myText1 = "Which non-academic room are you looking for?";
					myTextToSpeech.speak(myText1, TextToSpeech.QUEUE_FLUSH, null);
					Thread.currentThread();
					Thread.sleep(3000);
					intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Which non-academic room are you looking for?");
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block 
					e.printStackTrace();
				} 
 
			} 
			else if (prompt == 4){
				try { 
					myText1 = "What floor are you currently on?";
					myTextToSpeech.speak(myText1, TextToSpeech.QUEUE_FLUSH, null);
					Thread.currentThread();
					Thread.sleep(3000);
					intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "What floor are you currently on?");
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block 
					e.printStackTrace();
				} 
 
			} 
			else if (prompt == 5){
				try { 
					myText1 = "Would you like to use the stairs or elevator?";
					myTextToSpeech.speak(myText1, TextToSpeech.QUEUE_FLUSH, null);
					Thread.currentThread();
					Thread.sleep(3000);
					intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Would you like to use the stairs or elevator?");
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block 
					e.printStackTrace();
				} 
 
			} 
					 
			// Given an hint to the recognizer about what the user is going to say 
			intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
			        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
			 
			// Specify how many results you want to receive. The results will be sorted 
			// where the first result is the one with higher confidence. 
			intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
			 
			// Specify the recognition language. This parameter has to be specified only if the 
			// recognition has to be done in a specific language and not the default one (i.e., the 
			// system locale). Most of the applications do not have to set this parameter. 
			intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.ENGLISH);
			 
		    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
	    } 
	     
	    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    	if(requestCode == TTS_DATA_CHECK){
	    		if (resultCode == Engine.CHECK_VOICE_DATA_PASS){
		    		myTextToSpeech = new TextToSpeech(this, new OnInitListener() {
				  		public void onInit(int status){
				  			if (status == TextToSpeech.SUCCESS){
				  				ttsIsInit = true;
				  				if(myTextToSpeech.isLanguageAvailable(Locale.US) >= 0)
				  					myTextToSpeech.setLanguage(Locale.US);
				  				myTextToSpeech.setPitch(0.8f);
				  				myTextToSpeech.setSpeechRate(1.1f);
				  				startVoiceRecognitionActivity(); 
				  			} 
				  		} 
		    		}); 
	    		}