Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

118
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda