我想将音乐信息从我的播放器广播到锁屏小部件。我现在为了这些目的而存在 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);
关于android - 在没有 MediaPlayer 的情况下使用 RemoteControlClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22722362/