Jesbin MJ
Jesbin MJ

Reputation: 3249

changing ringtone with tts in android

I m trying to make an app in which when a incoming call comes identify the number and by using android text to speech it ll say the contact name of caller. i had done almost my problem is when call comes instead of default ring tone it should say the TTS.how to override ringtone with TTS here i m giving what i tried. can any one help me to get a better solution. i had tried the first answer now the ring volume got muted. but tts sound not coming.

   public class myPhoneStateChangeListener extends PhoneStateListener
{
    int ph_state = 0;
    speechcontact clsspcntct = new speechcontact();
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);
        if (state == TelephonyManager.CALL_STATE_RINGING)
        {

            String phoneNumber =   incomingNumber;
            String ContactName = objUtility.getContactName2(context,phoneNumber);

            if (RBSpkMde.isChecked())
            {
                speakWords(ContactName);
            }
        } 
    }
     public void speakWords(String speech)
    {
    myTTS.speak("you have call from"+speech, TextToSpeech.QUEUE_FLUSH, null);

    }
}

Upvotes: 0

Views: 1034

Answers (1)

Hoan Nguyen
Hoan Nguyen

Reputation: 18151

You mute the STREAM_RING

public class myPhoneStateChangeListener extends PhoneStateListener
{
    private int mRingVolume;
    Context context;

    public myPhoneStateChangeListener(Context cxt)
    {
        context = cxt;
    }

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);
        if (state == TelephonyManager.CALL_STATE_RINGING)
        {
            mRingVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
            mAudioManager.setStreamMute(AudioManager.STREAM_RING, true);
            String phoneNumber =   incomingNumber;
            String ContactName = objUtility.getContactName2(context,phoneNumber);

            if (RBSpkMde.isChecked())
            {
                speakWords(ContactName);
            }
        } 
        if (state == TelephonyManager.CALL_STATE_IDLE)
        {
             mAudioManager.setStreamMute(AudioManager.STREAM_RING, false);
             mAudioManager.setStreamVolume(AudioManager.STREAM_RING, 
                        mRingVolume, AudioManager.FLAG_ALLOW_RINGER_MODES);
        }
    }
     public void speakWords(String speech)
    {
    myTTS.speak("you have call from"+speech, TextToSpeech.QUEUE_FLUSH, null);

    }

}

Upvotes: 1

Related Questions