I know to some this might be an easy question or rather stupid but I cannot seem to get my head around this, how can I adjust volume via percentage lets say setting it to 70%? I know I have to get Maximum Stream Volume by using:
int streamMaxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
and We set it by using:
audioManager.setStreamVolume(AudioManager.STREAM_RING, streamMaxVolume);
but how can I set using Percentage? all answers will be highly appreciated, thank you.
Audio Manager in android is a class that provides access to the volume and modes of the device. Android audio manager helps us adjust the volume and ringing modes of devices based on our requirements. The modes that are well known to us, that are Ringing, Vibration, Loud, Silent, etc.
You can easily control your ringer volume and ringer profile i-e:(silent,vibrate,loud e.t.c) in android. Android provides AudioManager class that provides access to these controls. In order to use AndroidManager class, you have to first create an object of AudioManager class by calling the getSystemService() method.
This should work
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float percent = 0.7f;
int seventyVolume = (int) (maxVolume*percent);
audio.setStreamVolume(AudioManager.STREAM_MUSIC, seventyVolume, 0);
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