2

I am creating a casual game. And I applied sounds when the button is clicked etc.. now I have an image button of speaker icon. I Want to disable the sounds when It is pressed. how do I do that?

I used sound pool here is my sound loop class:


public class SoundPool {
    private static SoundPool soundpool;
    private static int clicksound;
    private static int gameosound;
    private static int scoreupsound;

    public SoundPlayer (Context context) {

        soundpool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
        clicksound = soundpool.load(context,R.raw.click, 1);
        gameosound= soundpool.load(context,R.raw.gameover, 1);
        scoreupsound = soundpool.load(context,R.raw.score, 1);
    }


    public void playclicksound() {
        soundpool.play(clicksound, 1.0f, 1.0f, 1, 0, 1.0f);
    }

    public void playgameosound() {
        soundpool.play(gameosound, 1.0f, 1.0f, 1, 0, 1.0f);
    }

    public void playscoreupsound() {
        soundpool.play(scoreupsound, 1.0f, 1.0f, 1, 0, 1.0f);
    }


}

        public class MainActivity {
        private TextView word1;
        private ImageButton speakericon;/* I want to change image when     pressed*/
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
             word1 = (TextView) findViewById(R.id.word1);
            
             word1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sound.playclicksound(); /* I want to cancel this                   sound when the speaker icon is pressed */
             });
            } 
Mr Singh
  • 3,936
  • 5
  • 41
  • 60
  • See related question https://stackoverflow.com/questions/11694110/how-to-stop-playing-a-sound-via-soundpool?rq=1 – Mihai8 Feb 24 '18 at 22:06

1 Answers1

0

Create a new class and add inside:

soundpool.pause();

or

soundpool.stop();

Something like this:

public void stopclicksound() {
        soundpool.stop();
    }
  • I also want to change the speaker icon when pressed. do I have to use an Image switcher? – Leonzo Escolar Feb 24 '18 at 22:50
  • You can find answer here: https://stackoverflow.com/questions/5089300/how-can-i-change-the-image-of-an-imageview – Ognjen Stefanovic Feb 24 '18 at 23:06
  • I want the image button to change when preesed. your link is completely different – Leonzo Escolar Feb 24 '18 at 23:32
  • I gave you an example, on how to change an image. You can implement it however you would like it. Here is how you can click on an image. https://stackoverflow.com/questions/7326601/android-imageview-on-click – Ognjen Stefanovic Feb 24 '18 at 23:35
  • @LeonzoEscolar I don't know your exact implementation. In order for `sound.sop()` to work, it has to be started already. Otherwise, it will give an error. I usually check if it has been started, before running `.stop()` function. But it works, I can assure you. You can see here: https://developer.android.com/guide/topics/media/mediaplayer.html and here as well: https://developer.android.com/reference/android/media/MediaPlayer.html#stop() – Ognjen Stefanovic Feb 26 '18 at 20:49