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

Viewed 4078

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%...

2 Answers

Same Trenton Telge answer in kotlin function

fun getCurrentAudioLevelPercentage(context: Context): Int {
    val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
    val currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
    val maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
    val percentage = 100 * currentVolume / maxVolume
    return percentage
}
Related