我想将音乐信息从我的播放器广播到锁屏小部件。为了这些目的,我现在存在remotecontrolclient类。但如果我使用自定义引擎播放音乐呢?如何在锁屏上控制小部件?

最佳答案

我找到了简单的方法。

//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);

10-06 08:46