在 Android 中静音全局声音
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10895882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mute the global sound in Android
提问by Yuen Tong
Is there a method that can be used to mute the global sound from an application button?
有没有一种方法可以使应用程序按钮的全局声音静音?
回答by WitnessApplications
They make it more complicated than it has to be. You can just use AudioManager.setStreamMute()
. Feel free to use the code below.
他们使它变得比它更复杂。你可以只使用AudioManager.setStreamMute()
. 随意使用下面的代码。
//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);
回答by Lokanath
The answer provided seems to be deprecated from android M (API 23) so this is provides an alternative solution
提供的答案似乎已从 android M (API 23) 中弃用,因此这提供了替代解决方案
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...
希望这可以帮助...
回答by Bhargav Panchal
There are four kinds of sound setting in Android:
Android中有四种声音设置:
- Alarm
- Music
- Ring tone
- Notification
- 警报
- 音乐
- 铃声
- 通知
First, create an object of the AudioManager
class:
首先,创建一个AudioManager
类的对象:
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);
回答by CodeNinja
You can use setRingerMode()method to do this
您可以使用setRingerMode()方法来做到这一点
AudioManager audioManager=(AudioManager)getSystemService(AUDIO_SERVICE);
audioManager.setRingerMode(audioManager.RINGER_MODE_SILENT);
mute & vibrate RINGER_MODE_VIBRATEflag
静音和振动RINGER_MODE_VIBRATE标志
unmute: RINGER_MODE_NORMAL
取消静音: RINGER_MODE_NORMAL
回答by Jasjit Singh Marwah
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.
从技术上讲,没有全局卷这样的东西。Bhargav 的回答应该让你走上正轨。AudioManager 类可以控制许多不同的“流”,即 Music、Ringer、In-Call、Alarm、Notification、System 和 DMTF。您必须将所有这些单独流的音量设置为 0,这是一个非常简单的过程,如图所示的 Bhargav。
Just create an Audiomanager object:-
只需创建一个 Audiomanager 对象:-
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.
不要输入 AudioManager.Stream,而是输入“AudioManager”。并按 Ctrl+空格键查看您可以使用的所有不同流。查看文档以了解许多不同类型的标志,这些标志可以在更改音量时添加声音或振动或调出音量 UI。
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.
每个流可能具有不同的最大音量,因此请确保使用 getMaxStreamVolume 获取最大值并相应地设置音量,以防您使用单个值来编辑所有流。所以如果用户想设置全局音量为50%,获取每个流的最大音量,乘以0.5,设置为对应的流。
回答by amr osama
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);