androidaudiovolume

How can I detect if the volume is set to mute?


When I am developing an Android application, how can I detect if volume is set to mute?

How can my application get notified if the volume changes to mute or gets unmuted?


Solution

  • You can use AudioManager to check volume is mute or not mute.

    AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    switch( audio.getRingerMode() ){
    case AudioManager.RINGER_MODE_NORMAL:
       break;
    case AudioManager.RINGER_MODE_SILENT:
       break;
    case AudioManager.RINGER_MODE_VIBRATE:
       break;
    }
    

    and for Volume change there is BroadcastReceiver for that.

    public class VolumeKeyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
           //Implement Logic
        }
    }
    

    Register the BroadcastReceiver

    VolumeKeyReceiver keyReceiver = new VolumeKeyReceiver();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("android.media.VOLUME_CHANGED_ACTION");
    registerReceiver(keyReceiver, intentFilter);