2

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);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eduardo de Souza
  • 21
  • 1
  • 1
  • 2

3 Answers3

3

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)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sunil Sunny
  • 3,949
  • 4
  • 23
  • 53
2

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


     
Muhafil Saiyed
  • 230
  • 1
  • 20
1

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);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Abhishek Patel
  • 4,280
  • 1
  • 24
  • 38
  • I will test these Codes when I back home, but Android manager doesnt mute the entire system? Another question, its good to make a class only for sound control? If so, how do i do that? – Eduardo de Souza Apr 06 '16 at 13:07
  • yes this idea is good..for that try `AsyncPlayer` ..http://alvinalexander.com/java/jwarehouse/android/media/java/android/media/AsyncPlayer.java.shtml – Abhishek Patel Apr 06 '16 at 13:10