如何处理来自 MediaSession 远程音量叠加的变化?

65
我正在尝试实现远程音量控制。使用硬件音量键已经可以控制音量,但是当我尝试在MediaSession远程音量叠加层中移动滑块时,VolumeProviderCompat.onAdjustVolume(..)回调函数没有被调用。我还尝试了其他回调函数,例如MediaSessionCompat.Callback.onMediaButtonEvent(..)或VolumeProviderCompat.onSetVolumeTo(..),但它们根本没有被调用。
如果您不知道我所说的“MediaSession远程音量叠加层”是什么,这里是一张截图:

enter image description here

我创建了一个演示项目,你可以在这里下载: 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));
            }
        };
    }

    ...
}

任何提示? 提前感谢!

你是否也需要实现onSetVolumeTo方法,就像这个类似的帖子中所示 https://dev59.com/w5vga4cB1Zd3GeqP2n33 - Jadent
1个回答

1
使用Activity的setVolumeControlStream方法——通常在其onCreate方法中——允许您指定在当前Activity处于活动状态时哪个音频流应由音量键控制:
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.audioplayer);

   setVolumeControlStream(AudioManager.STREAM_MUSIC);
}

您可以指定任何可用的音频流,但使用媒体播放器时,应指定STREAM_MUSIC流以使其成为音量键的焦点。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接