Mute the global sound in Android

2019-01-07 18:07发布

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

6条回答
神经病院院长
2楼-- · 2019-01-07 18:35

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

查看更多
仙女界的扛把子
3楼-- · 2019-01-07 18:42

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...

查看更多
霸刀☆藐视天下
4楼-- · 2019-01-07 18:44
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);
查看更多
萌系小妹纸
5楼-- · 2019-01-07 18:50

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);
查看更多
Explosion°爆炸
6楼-- · 2019-01-07 18:51

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);
查看更多
Animai°情兽
7楼-- · 2019-01-07 18:54

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.

查看更多
登录 后发表回答