14

In my Android application, I am playing different mp3 files using the MediaPlayer class.

The user is playing his/her own music with the default music application.

I want to:

  1. Pause the music of the default music application.
  2. Start playing music from my application.
  3. When my music is done playing, my app will resume the user's default music.

I want to pause and resume the music of the default music player from my application.

Is it possible to do this?

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
  • Refer to http://stackoverflow.com/a/37374679/2254969. The answers posted here are now old. – Ankit Aggarwal May 22 '16 at 13:02
  • Please find my complete solution for requesting audio focus and stopping background audio in different Android versions in this answer: https://stackoverflow.com/a/75508153/3873867 – Nijat Ahmadli Feb 22 '23 at 08:58

7 Answers7

12

The following code will pause a default MediaPlayer by sending a broadcast:

AudioManager mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);    

if (mAudioManager.isMusicActive()) {

    Intent i = new Intent("com.android.music.musicservicecommand");

    i.putExtra("command", "pause");
    YourApplicationClass.this.sendBroadcast(i);
} 
Mcingwe
  • 2,070
  • 2
  • 18
  • 17
  • I played an mp3 file in one application, and in another application I wrote this code. But it is not working. Is there any permission required? Or this works only inside one application? – AndroidDev Sep 09 '13 at 12:53
  • 1
    This is working only for Play music. is there is any other way that will work for all music app? @Mcingwe – Muneef M Jul 28 '17 at 10:36
7

To control currently playing music use this code.

    AudioManager mAudioManager = (AudioManager) c.getSystemService(Context.AUDIO_SERVICE);

if(mode == Config.MUSIC_NEXT) {
                KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT);
                mAudioManager.dispatchMediaKeyEvent(event);
            }else if(mode == Config.MUSIC_PLAY){
                KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY);
                mAudioManager.dispatchMediaKeyEvent(event);
            }
            else if(mode == Config.MUSIC_PREV){
                KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS);
                mAudioManager.dispatchMediaKeyEvent(event);
            }

infact this is the only code that is working for all music apps i used. checked with

  • Google play music.
  • Apple Music
  • OnePlus music app
Muneef M
  • 1,094
  • 15
  • 17
7

guys,try follow code,it worked for me:

// stop music player
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
am.requestAudioFocus(null,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

//resume music player

am.abandonAudioFocus(null);
aolphn
  • 2,950
  • 2
  • 21
  • 30
5
// pause
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "pause");
sendBroadcast(i);

// play
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "play");
sendBroadcast(i);

// next
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "next");
sendBroadcast(i);

// previous
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "previous");
sendBroadcast(i);

Some more info about available commands:

public static final String SERVICECMD = "com.android.music.musicservicecommand";
public static final String CMDNAME = "command";
public static final String CMDTOGGLEPAUSE = "togglepause";
public static final String CMDSTOP = "stop";
public static final String CMDPAUSE = "pause";
public static final String CMDPLAY = "play";
public static final String CMDPREVIOUS = "previous";
public static final String CMDNEXT = "next";
Ankit
  • 6,554
  • 6
  • 49
  • 71
Xar-e-ahmer Khan
  • 1,314
  • 15
  • 23
1

It is working very well for me :

    KeyEvent ke = new KeyEvent(KeyEvent.ACTION_DOWN, 
    KeyEvent.KEYCODE_MEDIA_PAUSE);
    Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);

    // construct a PendingIntent for the media button and unregister it
    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    PendingIntent pi = PendingIntent.getBroadcast(context,
            0/*requestCode, ignored*/, mediaButtonIntent, 0/*flags*/);
    intent.putExtra(Intent.EXTRA_KEY_EVENT, ke);
    sendKeyEvent(pi,context, intent);

    ke = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PAUSE);
    intent.putExtra(Intent.EXTRA_KEY_EVENT, ke);
    sendKeyEvent(pi, context, intent);
Sajid2067
  • 127
  • 1
  • 9
1

There is also another solution which could only pause music player which is playing :

  1. implements AudioManager.OnAudioFocusChangeListener
  2. override methods which is suggested by the ide.
  3. call this function when it is needed :

    private void checkAudioFocus() {
        AudioManager mAudioManager = (AudioManager) getApplicationContext().getSystemService(AUDIO_SERVICE);  
        if(mAudioManager.isMusicActive())
        {
            int result = mAudioManager.requestAudioFocus(AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
        }
    }
    
RaStall
  • 55
  • 1
  • 4
Sajid2067
  • 127
  • 1
  • 9
0

you try this code, hope help you.

val audioManager = requireActivity().getSystemService(Context.AUDIO_SERVICE) as AudioManager   val focusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN).run {
        setAudioAttributes(AudioAttributes.Builder().run {
            setUsage(AudioAttributes.USAGE_GAME)
            setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
            setOnAudioFocusChangeListener({  }, object : Handler() {})
            build()
        })
        setAcceptsDelayedFocusGain(true)
        build()
    }
    audioManager.requestAudioFocus(focusRequest)
Ho Binh
  • 351
  • 1
  • 3
  • 8