Search code examples
androiddevicevolume

How do I get the device audio level in percentage on Android?


I am trying to get my android app to check if the device's volume (media, not ringer) is lower than 'x' percent, but I am unsure how. I am currently trying this:

AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
int volume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
if (volume <0.7) {message};

Edit: I want the percentage of the volume, such as 10%/20%...


Solution

  • AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    int currentVolumePercentage = 100 * currentVolume/maxVolume;   
    

    currentVolumePercentage will be your percentage!