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();
}
}
});