T_V
T_V

Reputation: 17580

Media Player is playing after receiving call?

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?

Upvotes: 0

Views: 1238

Answers (1)

Vettiyanakan
Vettiyanakan

Reputation: 8470

Try this:

PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        if (state == TelephonyManager.CALL_STATE_RINGING) {
            //Incoming call: Pause music
        } else if(state == TelephonyManager.CALL_STATE_IDLE) {
            //Not in call: Play music
        } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
            //A call is dialing, active or on hold
        }
        super.onCallStateChanged(state, incomingNumber);
    }
};
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
    mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}

Upvotes: 1

Related Questions