Search code examples
javaandroidandroid-studioaudioandroid-mediaplayer

How to prevent my music player from starting after my phone rings and I hang up?


This code works. The only problem I have is when I'm NOT using the app and the phone rings, the music plays after I hang up.

public void level_one(View view){

        mp3 = MediaPlayer.create(this, R.raw.alpha_12);

        PhoneStateListener phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    mp3.pause();
                } else if(state == TelephonyManager.CALL_STATE_IDLE) {
                    mp3.start(); // Runs this line even if I didn't play
                } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
                    mp3.pause();
                }
                super.onCallStateChanged(state, incomingNumber);
            }
        };
        TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if(mgr != null) {
            mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    }

Solution

  • Just introduce a boolean which keeps track of whether the music was previously playing. I've just made up your surrounding class but you get the idea.

    public class MyClass
    {
        private boolean isMusicPlaying = false;
    
        public void someFunctionWhichStartsMusic()
        {
            //start the music
    
            isMusicPlaying = true;
        }
    
        public void level_one(View view){
    
            mp3 = MediaPlayer.create(this, R.raw.alpha_12);
    
            PhoneStateListener phoneStateListener = new PhoneStateListener() {
                @Override
                public void onCallStateChanged(int state, String incomingNumber) {
                    if (state == TelephonyManager.CALL_STATE_RINGING)
                    {
                        mp3.pause();
                    }
                    else if(state == TelephonyManager.CALL_STATE_IDLE
                              && isMusicPlaying) // pay attention to this!
                    {
                        mp3.start();
                    }
                    else if(state == TelephonyManager.CALL_STATE_OFFHOOK)
                    {
                        mp3.pause();
                    }
                    super.onCallStateChanged(state, incomingNumber);
                }
            };
            TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
            if(mgr != null) {
                mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
            }
        }
    }