Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 537
Davim Avatar asked Feb 24 '13 07:02

Davim


1 Answers

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);
like image 99
Ajay S Avatar answered Nov 06 '22 13:11

Ajay S