Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop other apps playing music from my current activity?

In my app I am trying to make a button which when clicked stops all other apps that are playing music. What I want is similar to that if I am playing music through one music player and if I try playing some other music through a second music player, the current music player stops and then the second one starts. How do we stop other apps playing music from current activity/app?

like image 654
Ujjwal Syal Avatar asked Mar 13 '13 15:03

Ujjwal Syal


People also ask

How do I stop apps from opening other apps on Android?

Tap on the app. On the next page, tap Open supported links and it will show you three options- “Allow app to open supported links, Always Ask, Don't allow app” to open links. You should select 'Don't allow the app to open links' to stop it from opening every time. Restart the phone, and that's it.

How do I stop Google Play Music from running in the background?

If you just tap the play/pause button in the app the song is only paused, so to completely stop and exit music player tap the android menu button to open the menu for the music player then tap “End” at the bottom of the menu, or alternatively if you pull the notifications panel down from the top of your screen you will ...

How do I stop an app from playing?

Here's how to kill background apps on Android: Go to Settings > Apps. Select an app you want to stop, then tap Force Stop. The app will relaunch when you restart your phone.


1 Answers

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) {
// other app had stopped playing song now , so u can do u stuff now .
}

Audio focus is assigned in turn to each application that requests it. This means that if another application requests audio focus, your application will lose it. You will be notifi ed of the loss of audio focus through the onAudioFocusChange handler of the Audio Focus Change Listener you registered when requesting the audio focus

      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;
}
}
};
like image 134
Pranav Jadav Avatar answered Oct 02 '22 15:10

Pranav Jadav