Mute the global sound in Android
Asked Answered
N

6

28

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

Nomadic answered 5/6, 2012 at 10:45 Comment(0)
M
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);
Meetinghouse answered 18/6, 2013 at 1:1 Comment(2)
Unfortunately deprecated since API level 22.Guatemala
@Endzeit: see @Lokanath's answer. For recent Android, just substitute in the adjustStreamVolume() call with ADJUST_MUTE.Ctenoid
D
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...

Diesis answered 27/8, 2016 at 11:0 Comment(1)
Perfect solution!! and it would be better if you keep single method mute/unmute flag args, but this is optional :).Maller
B
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);
Barquentine answered 5/6, 2012 at 10:59 Comment(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.Roundsman
There is no method like given in this answerKnighterrantry
E
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

Extrinsic answered 8/11, 2014 at 23:24 Comment(0)
D
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);
Domenech answered 23/11, 2016 at 16:54 Comment(0)
E
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.

Enfilade answered 21/6, 2012 at 13:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.