如何处理来自MediaSession远程卷叠加的更改?

我尝试实现远程音量控制。 它已经可以用硬件音量键控制音量。 但是当我尝试移动MediaSession远程卷覆盖的滑块时,不调用VolumeProviderCompat.onAdjustVolume(..)callback。 我也试过其他的callback像MediaSessionCompat.Callback.onMediaButtonEvent(..)VolumeProviderCompat.onSetVolumeTo(..)但他们根本不被调用。

如果您不知道“MediaSession远程音量叠加”的含义,请参阅以下屏幕截图: MediaSessionRemoteVolumeOverlay

我创build了一个演示项目,你可以在这里下载: https : //github.com/SaschaZ/VolumeProviderDemo

这里是我的DemoActivity的相关部分:

 public class DemoActivity extends AppCompatActivity { ... private Notification createNotification(@NonNull final DemoVolumeController demoVolumeController) { Log.d(TAG, "createNotification()"); final NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setPriority(NotificationCompat.PRIORITY_HIGH) .setSmallIcon(R.mipmap.ic_launcher); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (session != null) { session.release(); } session = new MediaSessionCompat(this, "demoMediaSession"); session.setPlaybackState(new PlaybackStateCompat.Builder() .setState(PlaybackStateCompat.STATE_PLAYING, 1, 1.0f) .build()); session.setPlaybackToRemote(createVolumeProvider(demoVolumeController)); session.setActive(true); } return builder.build(); } private VolumeProviderCompat createVolumeProvider(@NonNull final DemoVolumeController demoVolumeController) { // I don't use this callback directly, but i need to set it or my VolumeProvider will not work. (sounds // strange but i tried it several times) session.setCallback(new MediaSessionCompat.Callback() { @Override public boolean onMediaButtonEvent(final Intent mediaButtonEvent) { Log.d(TAG, "onMediaButtonEvent() called with: " + "mediaButtonEvent = [" + mediaButtonEvent + "]"); return super.onMediaButtonEvent(mediaButtonEvent); } }); return new VolumeProviderCompat(VolumeProviderCompat.VOLUME_CONTROL_RELATIVE, 100, demoVolumeController.getVolume()) { @Override public void onAdjustVolume(final int direction) { final int volume = demoVolumeController.setVolumeRelative(direction); showVolume(volume); Log.d(TAG, "onAdjustVolume() called with: " + "direction = [" + direction + "] - " + "new volume=" + volume); // Nasty hack to get sync with the volume overlay of android. setCurrentVolume does not work :( session.setPlaybackToRemote(createVolumeProvider(demoVolumeController)); } }; } ... } 

任何提示? 先谢谢你!

Related of "如何处理来自MediaSession远程卷叠加的更改?"