Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

122
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda