Gissipi_453
Gissipi_453

Reputation: 1340

Stopping background music player services like Soundcloud and Saavn using code

When I play 'Soundcloud' or 'Saavn' music playing app and I start my own app, the music in Saavn or Soundcloud is still playing in background. Need a way to stop it using code. How to do it ?

I tried this -

AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        String SERVICECMD = "com.android.music.musicservicecommand";
        String CMDNAME = "command";
        String CMDSTOP = "stop";

        if(mAudioManager.isMusicActive()) {
            Intent i = new Intent(SERVICECMD);
            i.putExtra(CMDNAME , CMDSTOP );
            HomeActivity.this.sendBroadcast(i);
        }

but it is only useful in stopping the music if Android's default music player is playing in background and not 3rd party apps like Saavn and Soundcloud.

Upvotes: 1

Views: 1382

Answers (2)

Pablo K
Pablo K

Reputation: 410

One possible solution I found on this is using the AudioManager.requestAudioFocus(...) function

AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

Code example:

int result = am.requestAudioFocus(focusChangeListener, //Request audio focus for playback
AudioManager.STREAM_MUSIC, //Use the music stream.
AudioManager.AUDIOFOCUS_GAIN); //Request permanent focus.

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's will lose it. The app is notified when there is a change on the AudioFocus, through the onAudioFocusChange event listener. This listener is the first parameter in the requestAudioFocus function.

This listener should look something like:

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 (not sure what ducking means really)
                break;
            case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) :
                //TODO: pause audio 
                break;
            case (AudioManager.AUDIOFOCUS_LOSS) :
                //TODO: pause audio
                break;
            case (AudioManager.AUDIOFOCUS_GAIN):
                //TODO: Return the volume to normal and resume if paused.
                break;
            default: break; //empty on default
        }
    }
};

Upvotes: 2

SaravInfern
SaravInfern

Reputation: 3388

I think it is not possible to stop a service of another app as you may not know the processid, You can stop the service of your app only if you know the Pid of the app using

android.os.Process.killProcess(processIdKillService)

Upvotes: 0

Related Questions