How to pause background music streaming on receiving call in android studio?

107 views Asked by At

I am using AudioManager and RingtoneManager to mute the background music and start ringing as soon as i receive a call. Which works quite good without issue. But once i take a call. As in below code , i am trying to end ringtone and start the call. But Some how it play muted music streaming long with voice call. How can i differentiate between music streaming and voice call and handle them.

Uri defaultRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE, defaultRingtoneUri);
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setStreamVolume(AudioManager.STREAM_MUSIC, am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
r = RingtoneManager.getRingtone(getApplicationContext(), defaultRingtoneUri);


am.setMode(AudioManager.MODE_IN_CALL);
                
 r.stop();
0

There are 0 answers