RemoteControlClient not appearing on lock screen

1k views Asked by At

So I'm using the following code in attempt to show metadata on Bluetooth AVRCP-Compatible devices, but haven't had any luck so far.

No controls are appearing on the lockscreen, which leads me to believe that the RemoteControlClient` isn't working as expected.

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

ComponentName rec = new ComponentName(getPackageName(), MediaButtonReceiver.class.getName());
mAudioManager.registerMediaButtonEventReceiver(rec);

Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
i.setComponent(rec);

PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
RemoteControlClient mRemoteControlClient = new RemoteControlClient(pi);

mAudioManager.registerRemoteControlClient(mRemoteControlClient);

int flags = RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
    | RemoteControlClient.FLAG_KEY_MEDIA_NEXT
    | RemoteControlClient.FLAG_KEY_MEDIA_PLAY
    | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
    | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
    | RemoteControlClient.FLAG_KEY_MEDIA_STOP;

mRemoteControlClient.setTransportControlFlags(flags);

mAudioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);

RemoteControlClient.MetadataEditor mEditor = mRemoteControlClient.editMetadata(true);
    mEditor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, "RCC Artist");
    mEditor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, "RCC Title");
    mEditor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, "RCC Album");
    mEditor.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, 6039000);
mEditor.apply();

mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);

I am executing this code when a button is pressed.

1

There are 1 answers

3
CommonsWare On BEST ANSWER

I'm using 5.0

Then RemoteControlClient will not work.

Does this mean it doesn't work at all?

Correct. Quoting the documentation:

Lock screens in Android 5.0 do not show transport controls for your MediaSession or RemoteControlClient. Instead, your app can provide media playback control from the lock screen through a notification. This gives your app more control over the presentation of media buttons, while providing a consistent experience for users across locked and unlocked devices.

Android 5.0 introduces a new Notification.MediaStyle template for this purpose. Notification.MediaStyle converts notification actions that you added with Notification.Builder.addAction() into compact buttons embedded in your app's media playback notifications. Pass your session token to the setSession() method to inform the system that this notification controls an ongoing media session.

Make sure to set the notification's visibility to VISIBILITY_PUBLIC to mark the notification as safe to show on any lock screen (secure or otherwise). For more information, see Lock screen notifications.