Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How large are the steps on onAdjustVolume of the VolumeProvider?

Tags:

android

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:

enter image description here

like image 223
casolorz Avatar asked Jul 19 '17 20:07

casolorz


1 Answers

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

  1. It's you to decide how many volume steps you want to provide.
  2. 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.
  3. 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...

like image 197
Amir Uval Avatar answered Oct 29 '22 08:10

Amir Uval