1

在 Android 12 之前,我使用下面的代码来检测按下的物理音量按钮以显示我的自定义 UI,但它在 Android 12 设备上停止工作,并且onAdjustVolume在我按下音量按钮时从未被调用:

        mediaSessionCompat = MediaSessionCompat(context, "My App")
    mediaSessionCompat?.isActive = true
    mediaSessionCompat?.setPlaybackState(
        PlaybackStateCompat.Builder()
        .setState(PlaybackStateCompat.STATE_PLAYING, PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 1f)
        .build())
    mediaSessionCompat?.setPlaybackToRemote(object :
        VolumeProviderCompat(VOLUME_CONTROL_ABSOLUTE, 7, 4) {
        override fun onAdjustVolume(direction: Int) {
            Log.v("My App", "Volume adjusted")
        }
    })
4

0 回答 0