Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjusting AudioManager Volume level Android

Tags:

android

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.

like image 307
Chrome Landos Avatar asked Oct 03 '15 19:10

Chrome Landos


People also ask

What is audio mode in android?

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.

How do I use audio Manager?

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.


1 Answers

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);
like image 117
varunkr Avatar answered Nov 15 '22 10:11

varunkr