How do I mute all sounds of my application?

你离开我真会死。 提交于 2019-12-23 02:40:21

问题


I'm creating a quiz app just for learning, and I've got a problem.

In my mainactivity I have a media player. It plays music even going through other activities, and I have a mute button to stop the music. I also have a sound effect when I successfully hit the right answer (on a question which is in other activity). But I don't know how to mute this SFX along with the background music when I press the mute button. Any hints? ;)

If it has a way to mute my entire application will be so good. I found some ways to mute, but this way will mute the entire system. And it's

AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, false);

回答1:


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);
         


//unmute audio
AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
             amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
         



回答2:


If you want to mute only the running apps sound, then you better do this:

MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1); //Use your mediaplayer instance instead of this. Also maintain a single instance of mediaplayer for the entire app.
    mp.start();

For Mute

mp.setvolume(0,0);

& Unmute or full volume

mp.setvolume(0,1);

Reference: http://developer.android.com/reference/android/media/MediaPlayer.html#setVolume(float, float)




回答3:


Try this. It may be of help to you.

AudioManager manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);    
manager.setStreamVolume(AudioManager.STREAM_ALARM, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);


来源:https://stackoverflow.com/questions/36441473/how-do-i-mute-all-sounds-of-my-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!