Android: MediaPlayer setVolume function

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!

Leave a Comment