In some of the cases in our application, we have to set our devices in Mute/Unmute mode. To get this functionality we have to add few lines of code. I have added two buttons one is for Mute and another one is for Unmute our device described below.
//This is to put your device into silent mode
btnMute.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
           AudioManager audioManager = (AudioManager)MainActivity.this.getSystemService(Context.AUDIO_SERVICE);
           audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
       }
});
//To put your device in a ringing mode
btnUnmute.setOnClickListener(new View.OnClickListener() {
	@Override
        public void onClick(View view) {
            AudioManager audioManager = (AudioManager)MainActivity.this.getSystemService(Context.AUDIO_SERVICE);
            audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
		
	    // To set full volume
            int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
            audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolume, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
        }
});
 
                       
                    
0 Comment(s)