Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pause Phone Ringtone while Speaking through Text To Speech and then Resume

I am making a caller speaking application which speak caller name using TTS. I want to pause the ringtone while TTS is speaking then resuming the ringtone. From what i have researched we can use AudioFocus (hope so). Anyway i am using 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 the Ringtone is stopped and tts is played but after tts is played Ringtone is not resumed. What should i do?

like image 342
Rohan Kandwal Avatar asked Jul 26 '13 20:07

Rohan Kandwal


1 Answers

Finally after scratching my head for two days i finally did it. For all those who want to implement something like this but are unable to do so 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);

}

explaination :-

ringVolume - gets the current volume of the ringtone .i.e ringtone volume set in the phone.

musicVolume - gets the current volume of the music

currentRingVolume just retains the ringVolume.

Note- STREAM_RING and STREAM_MUSIC are different things. See different volumes

Now the basic idea is to mute the ringtone while TTS is speaking and then set it to previous value.

seekBarValue- is my SeekBar which depicts the level of the TTS volume w.r.t musicVolume and is optional.

PauseRingtone- is a CheckBox Preference which checks whether we want to pause ringtone while speaking. If true is sets the AudioManager.STREAM_RING to 1 i.e. vibrate else ringVolume i.e. Phone Value, so both TTS and Ringtone play at the same time.

audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,musicVolume, 0) 

sets the volume of TTS to musicVolume. After TTS has completed speaking i.e. in onDone() we set the volume of Ringtone back to the ringVolume using currentRingVolume.

If my answer helped mark my answer useful.

like image 114
Rohan Kandwal Avatar answered Oct 19 '22 22:10

Rohan Kandwal