Pause the phone during a conversation through text to speech, and then resume

I am making a caller application that says the name of the caller using TTS. I want to pause the ringtone when TTS speaks and then resume the ringtone. From what I explored, we can use AudioFocus (hope so). Anyway I use the following code

Update

I am using this code now.

 public void speak(final String talk) throws InterruptedException { final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); int musicVolume= audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, musicVolume, 0); audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); int result = tts.setOnUtteranceProgressListener(new UtteranceProgressListener() { @Override public void onStart(String utteranceId) { // TODO Auto-generated method stub } @Override public void onError(String utteranceId) { // TODO Auto-generated method stub } @Override public void onDone(String utteranceId) { // TODO Auto-generated method stub audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); System.out.println("done"); } }); HashMap<String, String> params = new HashMap<String, String>(); params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"stringId"); tts.speak(talk, TextToSpeech.QUEUE_FLUSH, params); System.out.println("speaking after tts is over" + talk+" "+result); } 

Although Ringtone stopped and tts playing, but after playing tts Ringtone does not resume. What should I do?

+4
android text-to-speech ringtone android-audiomanager
Jul 26 '13 at 20:55
source share
2 answers

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) { // TODO Auto-generated method stub } @Override public void onError(String utteranceId) { // TODO Auto-generated method stub } @Override public void onDone(String utteranceId) { // TODO Auto-generated method stub System.out.println("done"); audioManager.setStreamVolume(AudioManager.STREAM_RING, currentRingVolume, 0); } }); HashMap<String, String> params = new HashMap<String, String>(); params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "stringId"); tts.speak(talk, TextToSpeech.QUEUE_FLUSH, params); System.out.println("speaking after tts is over" + talk + " " + result); } 

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. different volumes

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.

+8
Jul 27 '13 at 16:54
source share

Get rid of this listener and add this class phoneStateListener. This will solve your problem.

  int mode = audioManager.getRingerMode(); int musicVolume= audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, musicVolume, 0); audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); if(phoneState == TelephonyManager.CALL_STATE_IDLE) { audioManager.setRingerMode(mode); } 
+1
Jul 26 '13 at 21:52
source share



All Articles