Search code examples
androidmedia-player

Use RemoteControlClient without MediaPlayer


I want to broadcast music info from my player to the lock screen widget. I now the RemoteControlClient class exists for these purposes. But what if i'm using custom engine for music playback? How can i control widget on lock screen?


Solution

  • I found simple way to do this.

    //init remoteControlClient
        remoteControlClient = new RemoteControlClient(PendingIntent
                        .getBroadcast(this, 0, new Intent(Intent.ACTION_MEDIA_BUTTON).setComponent(componentName), 0));
        audioManager.registerRemoteControlClient(remoteControlClient);
                remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_NEXT |
                        RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE|RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS|
                        RemoteControlClient.FLAG_KEY_MEDIA_PAUSE|RemoteControlClient.FLAG_KEY_MEDIA_PLAY);
    //put info to the lockscreen
        remoteControlClient.editMetadata(true)
                        .putBitmap(MediaMetadataEditor.BITMAP_KEY_ARTWORK, bitmap)
                        .putString(MediaMetadataRetriever.METADATA_KEY_ARTIST,audio.getArtist())
                        .putString(MediaMetadataRetriever.METADATA_KEY_ALBUM,audio.getAlbum())
                        .putString(MediaMetadataRetriever.METADATA_KEY_TITLE,audio.getTitle()).apply();
    //destroy remoteCControlClient
        audioManager.unregisterRemoteControlClient(remoteControlClient);