Code example for TextToSpeech

Methods: setOnUtteranceProgressListener, speak

0
							@SuppressWarnings("serial") 
							@Override 
							public void onReceive(Context context, Intent intent) {
								if(intent.getExtras().getInt(AudioManager.EXTRA_SCO_AUDIO_STATE) == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
									context.unregisterReceiver(this);
									final TextToSpeech tts = new TextToSpeech(context, new OnInitListener() {									
										public void onInit(int status) {
											if(status != TextToSpeech.SUCCESS) {
												new AlertDialog.Builder(appContext).setTitle("Result").setMessage("Unable to Init").setNeutralButton("OK", null).show();
											} 
										} 
									}); 
									tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {	
										@Override 
										public void onStart(String utteranceId) {
	 
										} 
										 
										@Override 
										public void onError(String utteranceId) {
											new AlertDialog.Builder(appContext).setTitle("Result").setMessage("Error speaking: " + utteranceId).setNeutralButton("OK", null).show();
										} 
										 
										@Override 
										public void onDone(String utteranceId) {
											tts.shutdown();
											am.stopBluetoothSco();
											new AlertDialog.Builder(appContext).setTitle("Result").setMessage("Complete").setNeutralButton("OK", null).show();
										} 
									}); 
									tts.speak("New message received from Test Person: Hello, Ted.  This is a test message.  lol.", TextToSpeech.QUEUE_ADD, new HashMap<String, String>() {{
										put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_VOICE_CALL));
									}}); 
								} 
							} 
						}, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED));