0

当我创建一个 MediaStyle 通知时,它会公开一个选择媒体路由的按钮,如下所示: 参见 screenshot

我如何摆脱这个按钮?或者至少,如何将播放限制为仅本地设备(电话扬声器)?

以下是我创建通知的方式:

    MediaSessionCompat mediaSessionCompat = new MediaSessionCompat
            (this, TAG, mComponent, mediaButtonReceiverPendingIntent);
    mediaSessionCompat.setMetadata
            (new MediaMetadataCompat.Builder()
                    .putString(MediaMetadata.METADATA_KEY_TITLE, title)
                    .putString(MediaMetadata.METADATA_KEY_ALBUM, album)
                    .build()
            );
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder
            (this, CHANNEL_ID);
    notificationBuilder
            .setContentTitle(title)
            .setStyle(new MediaStyle()
                    .setMediaSession(mediaSessionCompat.getSessionToken())
                    .setShowActionsInCompactView(0, 1, 2));
4

1 回答 1

0

好的,我想我找到了解决方案。这是代码:

private VolumeProviderCompat getVolumeProvider() {
    final int VOLUME_UP = 1;
    final int VOLUME_DOWN = -1;
    return new VolumeProviderCompat(VolumeProviderCompat.VOLUME_CONTROL_RELATIVE, 1, 0) {
        @Override
        public void onAdjustVolume(int direction) {
            if (direction == VOLUME_UP) {
                plugin.sendNext();
            }
            else if (direction == VOLUME_DOWN) {
                plugin.sendPrevious();
            }
        }
    };
}

然后像这样使用它:

mediaSessionCompat.setPlaybackToRemote(getVolumeProvider());
于 2021-03-13T14:44:57.950 回答