I stream audio in my app. It's done but when I receive a call I should pause the stream until the call is finished, and then play the stream again. Is it possible to pause and play the stream while receiving a call in android?
-
http://stackoverflow.com/questions/5610464/stopping-starting-music-on-incoming-calls – Madi Jan 06 '16 at 07:23
4 Answers
You should use Audio Focus listeners. Phone state is NOT required to do this and is really bad practice (because the permission is totally privacy invasive).
The best thing about it is that you'll also receive a heads up when the user starts playing music in another app or when navigation is trying to say something.
There is a good doc on how to use it here:
http://developer.android.com/training/managing-audio/audio-focus.html

- 3,273
- 5
- 31
- 56
You can use the PhoneStateListener
to see the status of the phone, and pause your audio stream when the phone is in use. The android reference shows you the callbacks you can use, and this example shows you how to use it. Note that you'll need to add a permission to your manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE">
-
What did you try and in what way did it not work? Edit your question with code and details so that we can help you. – Tobbe Dec 13 '11 at 09:32
-
You're welcome, glad to help. Please mark the answer as accepted if it helped you with your problem! – Tobbe Dec 13 '11 at 10:37
implement this interface in your activity
AudioManager.OnAudioFocusChangeListener
on override method
override fun onAudioFocusChange(focusChange: Int) {
if (focusChange <= 0) {
//pause music
mediaPlayer!!.pause()
} else {
//play music
mediaPlayer!!.start()
}
and send a request when you have started your audio to playing
requestAudioFocus(
this,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN
)
this will handle all things messages, calls, etc. your audio will pause and start automatically.

- 39
- 8
Try this simpler code, I've tested this... (complete code is here http://blog.kerul.net/2012/01/how-to-pause-audio-while-call-is.html)
public class UrClassActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
private MediaPlayer mp;
...
...
...
//this is a code segmentation
//THis two lines of codes need to be inserted to the onCreate method
//This following codes dealing with the phone-state
//detect voice incoming call
//onCallStateChanged method will be executed if there's incoming
//or outcoming call
PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
//INCOMING call
//do all necessary action to pause the audio
if(mp!=null){//check mp
setPlayerButton(true, false, true);
if(mp.isPlaying()){
mp.pause();
}
}
} else if(state == TelephonyManager.CALL_STATE_IDLE) {
//Not IN CALL
//do anything if the phone-state is idle
} else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
//A call is dialing, active or on hold
//do all necessary action to pause the audio
//do something here
if(mp!=null){//check mp
setPlayerButton(true, false, true);
if(mp.isPlaying()){
mp.pause();
}
}
}
super.onCallStateChanged(state, incomingNumber);
}
};//end PhoneStateListener
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
...
...
...
}//end onCreate
}//end class