Android: MediaPlayer setVolume function

Qing picture Qing · Mar 7, 2011 · Viewed 95.3k times · Source

about the params Set what to make the player no sound and full sound

Thanks

Answer

ssuukk picture ssuukk · Aug 22, 2012

This function is actualy wonderful. Thanks to it you can create a volume scale with any number of steps!

Let's assume you want 50 steps:

int maxVolume = 50;

Then to set setVolume to any value in this range (0-49) you do this:

float log1=(float)(Math.log(maxVolume-currVolume)/Math.log(maxVolume));
yourMediaPlayer.setVolume(log1,log1); //set volume takes two paramater

Nice and easy! And DON'T use AudioManager to set volume! It will cause many side effects such as disabling silent mode, which will make your users mad!