Search code examples
androidandroid-mediaplayerandroid-audiomanager

Stop music of from Other APP


If in background the default music player is running and if I use my app to play music, both runs simultaneously.

Is there a way to stop background music player.


Solution

  • You will have to implement Mediaplayer's AudioFocus. To do this you need to get an instance of the AudioManager. Once you have the instance you can then use requestAudioFocus.

    AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);    
    // Request audio focus for playback
    int result = am.requestAudioFocus(focusChangeListener,
    // Use the music stream.
    AudioManager.STREAM_MUSIC,
    // Request permanent focus.
    AudioManager.AUDIOFOCUS_GAIN);   
    
    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            Log.d("AudioFocus", "Audio focus received");
            return true;
        } else {
            Log.d("AudioFocus", "Audio focus NOT received");
            return false;
        }
    }
    

    AudioFocus is assigned to each application that requests it. When your app receives focus it can pass an AudioManager.OnAudioFocusChangeListener which provides callbacks for when an focus change happens.

    If app gains audio focus, and another app requests audio focus, the focus will be given to the other app. Android will notify your app via an OnAudioFocusChangeListener so that your app can respond to the change. To receive focus events, you need to pass an instance of AudioManager.OnAudioFocusChangeListener like this.

    private OnAudioFocusChangeListener focusChangeListener =
              new OnAudioFocusChangeListener() {
                      public void onAudioFocusChange(int focusChange) {
                                 AudioManager am =(AudioManager)getSystemService(Context.AUDIO_SERVICE);
                        switch (focusChange) {
    
                               case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) :
                               // Lower the volume while ducking.
                               mediaPlayer.setVolume(0.2f, 0.2f);
                               break;
                               case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) :
                               pause();
                               break;
    
                               case (AudioManager.AUDIOFOCUS_LOSS) :
                               stop();
                               ComponentName component =new ComponentName(AudioPlayerActivity.this,MediaControlReceiver.class);
                               am.unregisterMediaButtonEventReceiver(component);
                               break;
    
                               case (AudioManager.AUDIOFOCUS_GAIN) :
                               // Return the volume to normal and resume if paused.
                               mediaPlayer.setVolume(1f, 1f);
                               mediaPlayer.start();
                               break;
                               default: break;
    }
    }
    };
    

    Reference Documentation: https://developer.android.com/guide/topics/media-apps/volume-and-earphones.html