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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With