Home » Android » android – MediaButtonReceiver not working with MediaBrowserServiceCompat

android – MediaButtonReceiver not working with MediaBrowserServiceCompat

Posted by: admin June 15, 2020 Leave a comment


I am trying to receive media button events from headsets or car controls (play/pause/etc.)

This is in my app manifest.

<service android:name=".mediaPlayerService.MediaPlayerService"
        <action android:name="android.media.browse.MediaBrowserService"/>

A receiver that will receive media buttons and send as
intents to your MediaBrowserServiceCompat implementation.
Required on pre-Lollipop. More information at
<receiver android:name="android.support.v4.media.session.MediaButtonReceiver">
        <action android:name="android.intent.action.MEDIA_BUTTON"/>

This is part of my MediaPlayerService

public class MediaPlayerService extends MediaBrowserServiceCompat {

public void onCreate()

private void initMediaSessions()
    mSession = new MediaSessionCompat(getApplicationContext(), MediaPlayerService.class.getSimpleName());

    mSession.setCallback(new MediaSessionCompat.Callback()
                            //callback code is here.     

public int onStartCommand(Intent startIntent, int flags, int startId)
    if (startIntent != null)
        // Try to handle the intent as a media button event wrapped by MediaButtonReceiver
        MediaButtonReceiver.handleIntent(mSession, startIntent);
    return START_STICKY;

It seems like I’m missing something. When I press the pause button on my headset controls, the onStartCommand is never called.

Any idea why this is not working as expected?

How to&Answers:

As explained in the Best Practices in media playback I/O 2016 talk, you need to also call


when you construct your media session and before you start playback, you must call setActive(true) as per its documentation:

You must set the session to active before it can start receiving media button events or transport commands.

Note that you must also call setActions() on your PlaybackStateCompat.Builder to say exactly what actions you support – if you don’t set that then you won’t get any of the callbacks relating to media buttons.