How large are the steps on onAdjustVolume of the VolumeProvider?

I'm implementing a VolumeProvider but I don't know what volume I should set my player to once onAdjustVolume is called. All it gives me is the direction. Right now I'm using getCurrentVolume() + direction but that only works if the steps are equal to AudioManager.ADJUST_LOWER or AudioManager.ADJUST_RAISE or AudioManager.ADJUST_SAME .

So how can I know what the steps will be?

Thanks.

Edit: I've created a sample to show the issue with @auval response.

public class MainActivity extends AppCompatActivity {
    private String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ProgressBar volumeBar = (ProgressBar) findViewById(R.id.progress);
        final VolumeProviderCompat volumeProviderCompat = new VolumeProviderCompat(VolumeProviderCompat.VOLUME_CONTROL_ABSOLUTE, 100, 100) {
            @Override
            public void onAdjustVolume(int direction) {
                Log.i(TAG, "onAdjustVolume " + direction);
                if (direction > 0) {
                    setCurrentVolume(getCurrentVolume() + 5);
                } else if (direction < 0) {
                    setCurrentVolume(getCurrentVolume() - 5);
                }
            }

            @Override
            public void onSetVolumeTo(int volume) {
                super.onSetVolumeTo(volume);
                Log.i(TAG, "onSetVolumeTo " + volume);
            }
        };
        volumeProviderCompat.setCallback(new VolumeProviderCompat.Callback() {
            @Override
            public void onVolumeChanged(VolumeProviderCompat volumeProvider) {
                volumeBar.setMax(volumeProvider.getMaxVolume());
                int currentVolume = volumeProvider.getCurrentVolume();
                Log.i(TAG,"onVolumeChanged " + currentVolume);
                volumeBar.setProgress(currentVolume);
            }
        });
        final MediaSessionCompat test = new MediaSessionCompat(this, "Test");
        PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder();
        stateBuilder.setActions(PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PLAY_PAUSE | PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_REWIND | PlaybackState.ACTION_FAST_FORWARD);
        stateBuilder.setState(PlaybackState.STATE_PLAYING, PlaybackState.PLAYBACK_POSITION_UNKNOWN, 1.0f);
        test.setPlaybackState(stateBuilder.build());
        test.setActive(true);
        test.setPlaybackToRemote(volumeProviderCompat);
    }
}

This is what happens on the screen:

在这里输入图像描述


It's better to use VolumeProviderCompat, because VolumeProvider requires minimum sdk of 21.

  • It's you to decide how many volume steps you want to provide.
  • Divide the max volume by this number, it will give you the value to adjust by on each step.
  • for example, max volume is 100, you decided to provide 5 steps ==> each step will change the volume by 20.
  • Adjust the volume accordingly.
  • for example, current volume is 40, you got volume up event ==> you need to set the volume now to 60.
  • By the way, nothing in the specifications states that volume control must be linear, you can decide to start with small interval change and increase the jump on each consecutive volume press...

    链接地址: http://www.djcxy.com/p/96850.html

    上一篇: 为什么Mockito无法在Kotlin中使用数字类型嘲讽通用参数类型?

    下一篇: VolumeProvider的onAdjustVolume上的步骤有多大?