Skip to content Skip to sidebar Skip to footer

Pause Song When Phone Rings

I am pretty new to android development. I wish to pause the multimedia whenever the phone rings and then start again when the call ends. how can that be done?

Solution 1:

What you want is a BroadcastReceiver, which is notified, when the phone receives a call. Here you can find a Tutorial on this.

Solution 2:

Here there is my implementation example:

publicclassMyActivityextendsActivityimplementsOnAudioFocusChangeListener {

    privateAudioManager mAudioManager;

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        ...
        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
        ...
    }

    @OverridepublicvoidonDestroy(){
         super.onDestroy();
         ...
         mAudioManager.abandonAudioFocus(this);
         ...
     }


    @OverridepublicvoidonAudioFocusChange(int focusChange) {
        if(focusChange<=0) {
            //LOSS -> PAUSE
        } else {
            //GAIN -> PLAY
        }
    }   
}

I hope it's helpful for you :-)

Solution 3:

Post a Comment for "Pause Song When Phone Rings"