Eduardo de Souza
Eduardo de Souza

Reputation: 21

How do I mute all sounds of my application?

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

Upvotes: 2

Views: 12125

Answers (3)

Muhafil Saiyed
Muhafil Saiyed

Reputation: 230

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


     

Upvotes: 2

Abhishek Patel
Abhishek Patel

Reputation: 4328

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

Upvotes: 1

Sunil Sunny
Sunil Sunny

Reputation: 3994

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)

Upvotes: 3

Related Questions