Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
android mute music on first button click and run it again on second button click

I have a simple app that has three buttons, you can increase a number using pulse button and decrease it using minus button and if this number become bigger than 10 the sound will start and if the number become less than 10 the sound will be paused.

and there is a third button to mute the sound that started from increasing and decreasing the number and run the sound when you click the same button again. this mute button should mute the sound on the first click and on the and run it again on second click.

my code is working fine but I had a little problem which is when I click the third button again the sound doesn't start it only mute the sound on the first click .

here my code for the three buttons:

    mp = MediaPlayer.create(this, R.raw.gg);

    final ImageButton btn3= (ImageButton) findViewById(R.id.imgBtnSound);

     btn3.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            clicked = true;

            if(clicked) {
                mp.setLooping(false);
                mp.pause();
                clicked = false;

            } else  if(clicked == false){
                mp.setLooping(true);
                mp.start();
                clicked = true;
            }


        }
    });



    tv = (TextView) findViewById(R.id.TextNumber);

    ImageButton btn1= (ImageButton) findViewById(R.id.imgBtnUp);
    ImageButton btn2= (ImageButton) findViewById(R.id.imgBtnDown);

    btn1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            result = result + 1;
            tv.setText("" + result);

            //check if need to play or not
            boolean needToPlay = result > 10;

            // if need to play, play when it's already not playing
            if (needToPlay && !mp.isPlaying()) {
                mp.setLooping(true);
                mp.start();
            }

        }
    });


    btn2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            result = result - 1;

            tv.setText("" + result);

            //check if need to play or not
            boolean needToPause = result <= 10;

            // if need to pause then pause only if it's playing
            if (needToPause && mp.isPlaying()) {
                mp.setLooping(false);
                mp.pause();
            }
        }
    });
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!