限制 MediaStyle 通知的媒体路由

Restrict media routing for MediaStyle notification

当我创建 MediaStyle 通知时,它会显示一个按钮来选择媒体路由,如下所示: see screenshot.

如何去掉这个按钮?或者至少,如何将播放限制为仅本地设备(phone 扬声器)?

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

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

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

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