How to control the default music player of android or any other player? By controlling i mean pause, play, next etc. Do i have to bind the service? I have tried to use the IMediaPlaybackService
but it is not working. There is certainly a way out as i have seen apps in the android market which controls the music player. Any idea?

- 2,664
- 6
- 25
- 38
7 Answers
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if(mAudioManager.isMusicActive()) {
Intent i = new Intent(SERVICECMD);
i.putExtra(CMDNAME , CMDSTOP );
YourApplicationClass.this.sendBroadcast(i);
}
you can by getting the audiomanager then sending commands to it.
these are the commands.
public static final String CMDTOGGLEPAUSE = "togglepause";
public static final String CMDPAUSE = "pause";
public static final String CMDPREVIOUS = "previous";
public static final String CMDNEXT = "next";
public static final String SERVICECMD = "com.android.music.musicservicecommand";
public static final String CMDNAME = "command";
public static final String CMDSTOP = "stop";

- 69,608
- 17
- 111
- 137

- 4,601
- 3
- 23
- 42
-
1Would you please tell me about how to play music, every other command is working, but couldn't figure out how to play music ?? Thank you :) – Abdellah Benhammou Jul 20 '14 at 15:37
-
2would you please tell how to find the SERVICECMD of all music players – Vinay Jun 01 '16 at 12:51
-
Thanks. It work well. Is it possible to take the picture using default camera app using same command? – Jame Nov 18 '16 at 12:12
-
@AbdellahBenhammou do you know, how to set shuffle and repeat mode in google play music. – Pratik Tank Dec 22 '17 at 12:28
-
thank you very much @Ahmed Ekri. you saved my lots of time :) – Oct 12 '18 at 09:00
Problem with sending Intent
for broadcast receiver is that, user has to open Music Player at least once, also, It does not work for many third party music players.
The code given below works on all music players.
long eventtime = SystemClock.uptimeMillis();
Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);
downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
sendOrderedBroadcast(downIntent, null);
Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);
upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
sendOrderedBroadcast(upIntent, null);
/*NEXT*/
Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
sendOrderedBroadcast(downIntent, null);
/*PREVIOUS*/
Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0);
downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
sendOrderedBroadcast(downIntent, null);
I have tested this code on Sony, Htc and Samsung devices.
This code imitates play/pause button action from hands-free(ear phones).

- 761
- 3
- 10
- 27

- 1,285
- 11
- 14
-
It will only play/pause music i guess. How will i implement features like next, previous ? – Naddy Sep 20 '13 at 11:37
-
Could you tell me how can I get details of the song which is playing? Do I get callback in the broadcast receiver to get the song details? – Pirate Oct 09 '14 at 04:51
-
Hi, I run this code in android 7 and it is working. My device is Huawei GR5 2017. But can I rely on this in a production app? Did anybody use this? Is it working in most of the android device and version? – Md. Tahmid Mozaffar Sep 24 '17 at 20:23
If you want to control the music player (the current playing one) there is a way to send KeyEvents to it:
if (mAudioManager == null) mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY);
mAudioManager.dispatchMediaKeyEvent(event);
This is a safer way, cause usually broadcasting keyEvent could cause to play more than one player at a time.
Note: it needs to have minSdk 19 or more.

- 55,411
- 20
- 125
- 222

- 506
- 7
- 8
-
1Just one more addition here: For few music apps (e.g. Spotify), you even have to dispatch "KeyEvent.ACTION_UP" event to work. – Arsalan Mehmood Nov 03 '17 at 11:16
Seems like the referenced KeyEvent methods don't work for me on the latest SDK. Using the AudioManager's dispatchMediaKeyEvent() method with a defined KeyEvent worked for me.
See: here
Code example:
this.mAudioManager = (AudioManager) this.context.getSystemService(Context.AUDIO_SERVICE);
long eventtime = SystemClock.uptimeMillis();
KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
mAudioManager.dispatchMediaKeyEvent(downEvent);
KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
mAudioManager.dispatchMediaKeyEvent(upEvent);

- 343
- 3
- 10
-
`downIntent` and `upIntent` aren't needed, and there's a typo in the `upIntent` line, but this works perfectly! Thanks! – TheWanderer Aug 12 '17 at 00:59
-
thanks @mpkasp! Any chance you know if we can get the info of the current playing song? Cheers – usernotnull Nov 01 '18 at 20:36
-
1@RJFares yeah set up a broadcast receiver and listen for music player changes ```mMusicReceiver = new BroadcastReceiver(this); mIntentFilter = new IntentFilter(); mIntentFilter.addAction("com.apple.android.music.metachanged"); mIntentFilter.addAction("com.apple.android.music.playstatechanged");``` you can get the track, album, etc by `this.track = intent.getStringExtra("track");` etc – mpkasp Nov 02 '18 at 21:39
KeyEvent's don't work properly in newest Android, as well as don't support applications like Spotify. The best solution is to use MediaController. For each application that is currently playing and you want to control it you have to create different MediaController. It requires Context and MediaSession.Token. Token 'identifies' process that plays music. For example if you played Google Play Music and you now play Spotify you can create MediaControllers for both. How to retrieve tokens? There are two ways:
- Get list from MediaSessionManager. You can find it in Google example project
- Get it from Notifications - if you have access to Notifications (NotificationListenerService) you can search for notifications from music apps and get their current Token.
You check current state of session from MediaController:
getPlaybackState().getState() //e.g. PlaybackStateCompat.STATE_PLAYING
and control app using transport controls, e.g.:
getTransportControls().skipToNext()
Depending on your minSDK use Compat or not version. You can cast MediaSession.Token to MediaSessionCompat.Token with:
MediaSessionCompat.Token.fromToken(token)
-
I love how this is still relevant a year and a half later. It literally turned a 2 line command into a whole architecture. I'm just trying to skip what's playing in the background and the KeyEvent approach isn't reliable, especially once you disconnect from the device in interest, such as playing from google cast. – Gary May 30 '20 at 03:28
There's a new method in Android 4.4 called RemoteController, but it's a pain in the arse and I couldn't get it to work. It also requires you to direct the user to the global security settings and allow you app access to all their notifications!!! I'm not sure what Google was thinking here.
Anyway I followed all the instructions but registerRemoteController()
still returns false
so there's little else I can do (apparently Google have never heard of error codes).
Given that user2572182's method doesn't seem to work on Android 4.4 it seems this is impossible.
Edit: I found an example app using RemoteController
on XDA of all places (who knew there was useful content there?). It works on my phone, but you still need to tell your users to go and change some obscure security setting so it's not ideal:
http://forum.xda-developers.com/showthread.php?p=51535568
Edit 2: Google have changed this again in Android "L". I guess they realised RemoteControlClient
was too complicated.

- 88,195
- 71
- 364
- 509
Solution for all latest and older versions (below kitkat to Android 10).
/*global variable for audiomanager*/
AudioManager mAudioManager;
/*initalize audiomanager in oncreate or onCreateView if using fragment*/
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
/*trigger keyEvent to play system music */
KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
mAudioManager.dispatchMediaKeyEvent(event);
}
else
{
Intent i = new Intent(SERVICECMD);
i.putExtra(CMDNAME , CMDPLAY );
sendBroadcast(i);
}
/*you can use same code for stop system music but you need to change MEDIA_PLAY to MEDIA_STOP*/

- 155
- 1
- 11