Mike James Johnson
Mike James Johnson

Reputation: 734

Simple way to implement a mute button in Android app

I am looking for a simple way to implement a mute button.

Obviously I could have a toggle button, mute_button and in every instance where a sound is played I could check:

if(mute_button.isChecked()){
     //dont play
}else{
    //play the sound
}

But I have a lot of instances that I would need to sort through and add this in. Is there a simpler way of something along the lines of:

    mute_button.setOnClickListener() ...

onClick
    if(isChecked){
        //set entire application to mute mode
    }else{
       //keep default application sound settings
    }

So, really I am looking for a "higher level" or application wide mute, rather than checking if a button is checked every single time a sound may play.

Upvotes: 3

Views: 2017

Answers (1)

Garry
Garry

Reputation: 4533

You can try by setting the media volume to zero

MediaPlayer media = MediaPlayer.create(this, R.raw.backgroundSound);
//media.setVolume(leftVolume , rightVolume);
media.setVolume(0 , 0);

For soundpool you can try like this:

soundpool.setVolume(streamID, 0f, 0f);

Upvotes: 1

Related Questions