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


in app trying make button when clicked stops other apps playing music. want similar if playing music through 1 music player , if try playing other music through second music player, current music player stops , second 1 starts. how stop other apps playing music current activity/app?

audiomanager = (audiomanager)getsystemservice(context.audio_service);  // request audio focus playback int result = am.requestaudiofocus(focuschangelistener, // use music stream. audiomanager.stream_music, // request permanent focus. audiomanager.audiofocus_gain);   if (result == audiomanager.audiofocus_request_granted) { // other app had stopped playing song , u can u stuff . } 

audio focus assigned in turn each application requests it. means if application requests audio focus, application lose it. notifi ed of loss of audio focus through onaudiofocuschange handler of audio focus change listener registered when requesting audio focus

      private onaudiofocuschangelistener focuschangelistener =           new onaudiofocuschangelistener() {                   public void onaudiofocuschange(int focuschange) {                              audiomanager =(audiomanager)getsystemservice(context.audio_service);                     switch (focuschange) {                             case (audiomanager.audiofocus_loss_transient_can_duck) :                            // lower 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 volume normal , resume if paused.                            mediaplayer.setvolume(1f, 1f);                            mediaplayer.start();                            break;                            default: break; } } }; 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -