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

2

5

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?

Milline answered 26/7, 2013 at 20:55 Comment(0)
M
9

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.

Milline answered 27/7, 2013 at 16:54 Comment(2)
I want to stop or pause the music player if tts starts working and restart the music player if tts stops.can you plz help me in this.Kaluga
The coding logic is same as I have written, I was pausing ringtone, you need to pause music player. If you are still unable to get an answer, best option would be to ask a new question.Milline
I
1

Get rid of this focus listener and add this code phoneStateListener class. 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);

}
Itin answered 26/7, 2013 at 21:52 Comment(2)
The code is making no ringtone to be played other than tts. But i want to play the ringtone after tts has stopped speaking. How to do that?Milline
I used setOnUtteranceProgressListener on tts and then in onDone() i used audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);. But that only put the phone in normal mode. There is no ringtone.Milline

© 2022 - 2024 — McMap. All rights reserved.