Finally, scratching my head for two days, I finally did it. For all those who want to implement something like this, but can't do it, here is the code
public void speak(final String talk) throws InterruptedException { final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); int ringVolume = audioManager .getStreamVolume(AudioManager.STREAM_RING); int musicVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); currentRingVolume = ringVolume; musicVolume = (int) ((musicVolume * seekbarValue) / 100); if (PauseRingtone == true) { audioManager.setStreamVolume(AudioManager.STREAM_RING, 1, AudioManager.FLAG_SHOW_UI); } else { audioManager.setStreamVolume(AudioManager.STREAM_RING, ringVolume, AudioManager.FLAG_SHOW_UI); } audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, musicVolume, 0); int result = tts .setOnUtteranceProgressListener(new UtteranceProgressListener() { @Override public void onStart(String utteranceId) {
explanation: -
ringVolume - Gets the current volume volume of the .ie ringtone installed on the phone.
musicVolume - Gets the current volume of music
currentRingVolume just saves ringVolume .
Note. STREAM_RING and STREAM_MUSIC are two different things. Cm. 
Now the main idea is mute ringtone , and TTS says, and then sets it to the previous value.
seekBarValue is my SeekBar that displays the TTS volume level of wrt musicVolume and is optional.
PauseRingtone is a CheckBox Preference that checks if we want to pause the melody during a call. If true sets AudioManager.STREAM_RING to 1 ie vibrate else ringVolume ie Phone Value , so that both TTS and ringtone played simultaneously.
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,musicVolume, 0)
sets the TTS volume in musicVolume . After the completion of the TTS , i.e. In onDone() , we set the ringtone volume back to ringVolume using currentRingVolume .
If my answer helped mark my answer useful.
Rohan Kandwal Jul 27 '13 at 16:54 2013-07-27 16:54
source share