Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Mute/Unmute device sound programmatically in android?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 9.91k
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: