使用不带MediaPlayer的RemoteControlClient

问题描述 投票:3回答:1

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

android media-player
1个回答
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);
© www.soinside.com 2019 - 2024. All rights reserved.