Reputation: 33
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?
Upvotes: 2
Views: 4093
Reputation: 1831
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">
Upvotes: 3
Reputation: 89
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.
Upvotes: 0
Reputation: 3275
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
Upvotes: 5
Reputation: 99
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
Upvotes: 0