Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mute the global sound in Android

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

like image 829
Yuen Tong Avatar asked Jun 05 '12 10:06

Yuen Tong


People also ask

How do I mute the sound on my phone?

If you have an Android phone, you can mute your phone from the call screen. Your call screen has different buttons including a mute button (circled below). It is a microphone with a slash line through it. Please click on this button to mute and unmute your hone.

How do you mute all apps with sound?

So if you'd like to get rid of this toast message, open App Volume Control and tap the settings icon at the top of the screen. From here, simply disable the "Notify with a notification" option, and from now on, your volume levels will change automatically and silently when you open any of the apps you configured.


2 Answers

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); 
like image 100
WitnessApplications Avatar answered Sep 30 '22 14:09

WitnessApplications


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

like image 24
Lokanath Avatar answered Sep 30 '22 14:09

Lokanath