27

Is there a method that can be used to mute the global sound from an application button?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yuen Tong
  • 789
  • 3
  • 9
  • 11

6 Answers6

45

They make it more complicated than it has to be. You can just use AudioManager.setStreamMute(). Feel free to use the code below.

//mute audio
AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
             amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
             amanager.setStreamMute(AudioManager.STREAM_ALARM, true);
             amanager.setStreamMute(AudioManager.STREAM_MUSIC, true);
             amanager.setStreamMute(AudioManager.STREAM_RING, true);
             amanager.setStreamMute(AudioManager.STREAM_SYSTEM, true);


//unmute audio
AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
             amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
             amanager.setStreamMute(AudioManager.STREAM_ALARM, false);
             amanager.setStreamMute(AudioManager.STREAM_MUSIC, false);
             amanager.setStreamMute(AudioManager.STREAM_RING, false);
             amanager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
Pang
  • 9,564
  • 146
  • 81
  • 122
23

The answer provided seems to be deprecated from android M (API 23) so this is provides an alternative solution

public void MuteAudio(){
    AudioManager mAlramMAnager = (AudioManager) aActivity.getSystemService(aContext.AUDIO_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        mAlramMAnager.adjustStreamVolume(AudioManager.STREAM_NOTIFICATION, AudioManager.ADJUST_MUTE, 0);
        mAlramMAnager.adjustStreamVolume(AudioManager.STREAM_ALARM, AudioManager.ADJUST_MUTE, 0);
        mAlramMAnager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_MUTE, 0);
        mAlramMAnager.adjustStreamVolume(AudioManager.STREAM_RING, AudioManager.ADJUST_MUTE, 0);
        mAlramMAnager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_MUTE, 0);
    } else {
        mAlramMAnager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
        mAlramMAnager.setStreamMute(AudioManager.STREAM_ALARM, true);
        mAlramMAnager.setStreamMute(AudioManager.STREAM_MUSIC, true);
        mAlramMAnager.setStreamMute(AudioManager.STREAM_RING, true);
        mAlramMAnager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
    }
}

public void UnMuteAudio(){
    AudioManager mAlramMAnager = (AudioManager) aActivity.getSystemService(aContext.AUDIO_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        mAlramMAnager.adjustStreamVolume(AudioManager.STREAM_NOTIFICATION, AudioManager.ADJUST_UNMUTE, 0);
        mAlramMAnager.adjustStreamVolume(AudioManager.STREAM_ALARM, AudioManager.ADJUST_UNMUTE, 0);
        mAlramMAnager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_UNMUTE,0);
        mAlramMAnager.adjustStreamVolume(AudioManager.STREAM_RING, AudioManager.ADJUST_UNMUTE, 0);
        mAlramMAnager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_UNMUTE, 0);
    } else {
        mAlramMAnager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
        mAlramMAnager.setStreamMute(AudioManager.STREAM_ALARM, false);
        mAlramMAnager.setStreamMute(AudioManager.STREAM_MUSIC, false);
        mAlramMAnager.setStreamMute(AudioManager.STREAM_RING, false);
        mAlramMAnager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
    }
}

hope this helps...

Lokanath
  • 662
  • 5
  • 18
  • Perfect solution!! and it would be better if you keep single method mute/unmute flag args, but this is optional :). – Pankaj Kumar Jan 10 '18 at 12:35
11

There are four kinds of sound setting in Android:

  • Alarm
  • Music
  • Ring tone
  • Notification

First, create an object of the AudioManager class:

AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);

If you want to set the volume, use these:

For notification

amanager.setStreamVolume(AudioManager.STREAM_NOTIFICATION,
    AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);

For alarm

amanager.setStreamVolume(AudioManager.STREAM_ALARM,
    AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);

For music

amanager.setStreamVolume(AudioManager.STREAM_MUSIC,
    AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);

For ringtone

amanager.setStreamVolume(AudioManager.STREAM_RING,
    AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
Pang
  • 9,564
  • 146
  • 81
  • 122
Bhargav Panchal
  • 1,159
  • 1
  • 12
  • 27
  • 2
    Aren't there actually MORE streams (system, voice call)? I've seen those used in real devices. Also, there's no method AudioManager#setStreamVolume(int, int), only one that accepts three parameters exists (one being the volume to set). You could pass 0, but this isn't accepted for all streams. – Tom Sep 30 '14 at 22:44
  • There is no method like given in this answer – Zar E Ahmer Jan 09 '15 at 11:08
4

You can use setRingerMode() method to do this

AudioManager audioManager=(AudioManager)getSystemService(AUDIO_SERVICE);
audioManager.setRingerMode(audioManager.RINGER_MODE_SILENT);

mute & vibrate RINGER_MODE_VIBRATE flag

unmute: RINGER_MODE_NORMAL

CodeNinja
  • 1,168
  • 2
  • 14
  • 27
4
mAlramMAnager.adjustStreamVolume(AudioManager.STREAM_NOTIFICATION, AudioManager.ADJUST_TOGGLE_MUTE, 0);
mAlramMAnager.adjustStreamVolume(AudioManager.STREAM_ALARM, AudioManager.ADJUST_TOGGLE_MUTE, 0);
mAlramMAnager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_TOGGLE_MUTE,0);
mAlramMAnager.adjustStreamVolume(AudioManager.STREAM_RING, AudioManager.ADJUST_TOGGLE_MUTE, 0);
mAlramMAnager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_TOGGLE_MUTE, 0);
amr osama
  • 1,129
  • 2
  • 18
  • 34
3

Technically there is no such thing as a global volume. Bhargav's answer should put you on the right track. There are many different "streams" that can be controlled by the AudioManager class, namely Music, Ringer, In-Call, Alarm, Notification, System and DMTF. You have to set the volume for all of these individual streams to 0, which is an incredibly simple process as Bhargav as shown.

Just create an Audiomanager object:-

AudioManager audioManager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);

And then use it to change the volume for all the streams:-

audioManager.setStreamVolume(AudioManager.Stream, Extra Flags or null);

Instead of typing AudioManager.Stream, type "AudioManager." and press Ctrl+Space Bar to see all of the different streams you can use. Check the documentation for the many different types of flags that can add a sound or a vibration or bring up the volume UI when the volume is changed.

Each stream may have a different maximum volume so be sure to use getMaxStreamVolume to get the maximum values and set the volume accordingly in case you are using a single value to edit all the streams. So if the user wants to set global volume to 50%, get the maximum volume for each stream, multiply them by 0.5, and set it to the corresponding streams.