使用不带MediaPlayer的RemoteControlClient

时间:2014-03-28 20:13:39

标签: android media-player

我想将播放器中的音乐信息广播到锁定屏幕小部件。我现在RemoteControlClient类用于这些目的。但是,如果我使用自定义引擎进行音乐播放呢?如何在锁定屏幕上控制小部件?

1 个答案:

答案 0 :(得分:1)

我找到了简单的方法。

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