Search code examples
androidaudiomedia-player

Android: MediaPlayer setVolume function


About the params, what do I need to do to make the player make no sound and to make full sound.


Solution

  • 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!