I am using this code, to listen the state of phone. when call arrives it pauses the media player but as soon as I pick up the call it starts play again from listening speaker(not from ringer). and I also tried with removing the mediaPlayer.start()
from case TelephonyManager.CALL_STATE_IDLE:
in this case it works fine but it doesn't start(resume) again. Is there any flag available to do that?
private final PhoneStateListener phoneListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
try {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(context, "Call is Coming",Toast.LENGTH_SHORT).show();
mediaPlayer.pause();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
case TelephonyManager.CALL_STATE_IDLE:
mediaPlayer.start();
break;
default:
}
} catch (Exception ex) {
mediaPlayer.release();
}
}
};
}
or Is there any other way to do that?