Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Minimal MediaSessionCompat needed to get RemoteControlClient to appear
package com.example.remotecontrolclient;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.os.IBinder;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;
public class PlayerService extends Service {
private MediaSessionCompat mediaSession;
@Override
public void onCreate() {
super.onCreate();
ComponentName receiver = new ComponentName(getPackageName(), RemoteReceiver.class.getName());
mediaSession = new MediaSessionCompat(this, "PlayerService", receiver, null);
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSession.setPlaybackState(new PlaybackStateCompat.Builder()
.setState(PlaybackStateCompat.STATE_PAUSED, 0, 0)
.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE)
.build());
mediaSession.setMetadata(new MediaMetadataCompat.Builder()
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, "Test Artist")
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, "Test Album")
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, "Test Track Name")
.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, 10000)
.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART,
BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.build());
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.requestAudioFocus(new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
// Ignore
}
}, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
mediaSession.setActive(true);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (mediaSession.getController().getPlaybackState().getState() == PlaybackStateCompat.STATE_PLAYING) {
mediaSession.setPlaybackState(new PlaybackStateCompat.Builder()
.setState(PlaybackStateCompat.STATE_PAUSED, 0, 0.0f)
.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE).build());
} else {
mediaSession.setPlaybackState(new PlaybackStateCompat.Builder()
.setState(PlaybackStateCompat.STATE_PLAYING, 0, 1.0f)
.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE).build());
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
mediaSession.release();
}
}
package com.example.remotecontrolclient;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
public class RemoteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event != null && event.getAction() == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
context.startService(new Intent(context, PlayerService.class));
break;
}
}
}
}
}
@Jitan

This comment has been minimized.

Copy link

@Jitan Jitan commented Jul 15, 2015

Thanks for this. Not a lot of examples around.

@MaciejCzekanski

This comment has been minimized.

Copy link

@MaciejCzekanski MaciejCzekanski commented Dec 17, 2015

Unfortunately, does not work for me. I tested on Nexus 5 (Marshmallow) and Moto G (Lollipop). Using appcompat v 23.1.1.

@jeffbordon

This comment has been minimized.

Copy link

@jeffbordon jeffbordon commented Feb 7, 2016

I can confirm that it is not working on Nexus 5 with Marshmallow, however it is working on Kitkat in Visual Studio Emulator. Any ideas whats wrong on Marshmallow?

@nospam2k

This comment has been minimized.

Copy link

@nospam2k nospam2k commented Jun 3, 2016

FIX:
In onCreate before AudioManager:
mediaSession.setCallback(mMediaSessionCallback);

  Add this in outside of onCreate:

private final MediaSessionCompat.Callback mMediaSessionCallback
        = new MediaSessionCompat.Callback() {

    @Override
    public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
        final String intentAction = mediaButtonEvent.getAction();
        if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
            final KeyEvent event = mediaButtonEvent.getParcelableExtra(
                    Intent.EXTRA_KEY_EVENT);
            if (event == null) {
                return super.onMediaButtonEvent(mediaButtonEvent);
            }
            final int keycode = event.getKeyCode();
            final int action = event.getAction();
            if (event.getRepeatCount() == 0 && action == KeyEvent.ACTION_DOWN) {
                switch (keycode) {
                    // Do what you want in here
                    case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                        Log.d("MPS", "KEYCODE_MEDIA_PLAY_PAUSE");
                        break;
                    case KeyEvent.KEYCODE_MEDIA_PAUSE:
                        Log.d("MPS", "KEYCODE_MEDIA_PAUSE");
                        break;
                    case KeyEvent.KEYCODE_MEDIA_PLAY:
                        Log.d("MPS", "KEYCODE_MEDIA_PLAY");
                        break;
                }
            }
        }
        return super.onMediaButtonEvent(mediaButtonEvent);
    }
};

It looks like Android 21+ (at least for me) doesn't hit the BroadCastReceiver. This does.

@immran

This comment has been minimized.

Copy link

@immran immran commented Jul 6, 2016

Hi All, I have used the same code which is there above. I can see the notifications, but i can't control it.. The MediaEvent is not triggering and It doesn't recognize when I pressed Pause, Previous .etc.
Help Needed. Thanks

@hungdh0x5e

This comment has been minimized.

Copy link

@hungdh0x5e hungdh0x5e commented Jul 23, 2016

@immran Do you register <action android:name="android.intent.action.MEDIA_BUTTON"/> for RemoteReceiver in Manifests ?

@Mavamaarten

This comment has been minimized.

Copy link

@Mavamaarten Mavamaarten commented Sep 28, 2016

I have found that the image does not show up on Marshmallow when the PlaybackState is set to PAUSED (which is the default state in this example). It does show when the state is PLAYING.

@MilanNz

This comment has been minimized.

Copy link

@MilanNz MilanNz commented Oct 1, 2016

I have got album art and notification on lock screen with this code,but there are no buttons. I'm using Android 6.0. Do you have any idea?

@vyshnavkr

This comment has been minimized.

Copy link

@vyshnavkr vyshnavkr commented Jan 17, 2017

Anyone used MediaSessionCompat with MediaBrowserServiceCompat?

@vyshnavkr

This comment has been minimized.

Copy link

@vyshnavkr vyshnavkr commented Jan 30, 2017

putLong(MediaMetadataCompat.METADATA_KEY_DURATION, 10000); duration is displayed as 10, which I believe its because dividing 10000 by 1000 to get seconds format. But why 10000? How do I get the original duration of the track being played?

@ishaan-khan

This comment has been minimized.

Copy link

@ishaan-khan ishaan-khan commented Feb 22, 2017

I have implemented the above example but it does not seem to show anything on the lockscreen. I am receiving the mediasession callbacks but the nothings seems to show up on the lock screen. Any ideas what is going wrong?
FYI: I am using support library 25.1.0

@nebular

This comment has been minimized.

Copy link

@nebular nebular commented Apr 17, 2017

I am using this in an Activity and successfully receive the media buttons. However, in the lock screen nothing is displayed (even though the activity is in the background and with the mediaplayer active). Does the lockscreen functionality require a service?

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented May 11, 2017

How can I add SongList to the Queue of the session from activity? I can not see any kind of post regarding it.
I tried

mediaSessionCompat = MediaSessionCompat.fromMediaSession(mainActivity,mediaBrowserCompat.getSessionToken());
mediaSessionCompat.setQueue(queueList);

gives an error mediaSession is not a valid MediaSession object

@gregko

This comment has been minimized.

Copy link

@gregko gregko commented Aug 31, 2017

I created a trivial working app project from this Gist at https://github.com/gregko/PlayerServiceSample. In the LogCat output one can see a message when a media button is pressed on a headset, when running on Android 5.x through 7.x. However it fails completely on Android 8 "Oreo", which final build Google just released. Would appreciate any hints on how to make this work under "Oreo", thanks!

@mehulTank

This comment has been minimized.

Copy link

@mehulTank mehulTank commented Jul 2, 2018

i am getting same issue i have created one app in that one bluetooth device is bind with phone and according to event its working fine in below oreo but in that oreo not getting any action event if any one idea help me to solve out

@pavelpoley

This comment has been minimized.

Copy link

@pavelpoley pavelpoley commented Oct 29, 2018

I cannot change to "play" button, the button always show "pause", if i change to state to paused the playback screen not showing anything

This code not show anything
mediaSession.setPlaybackState(new PlaybackStateCompat.Builder()
.setState(PlaybackStateCompat.STATE_PAUSED, 0, 0.0f)
.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE).build());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment