I am trying to create a method of an image button. When it's clicked, the background music stops and the image button change to another image. When pressed again, it will return as if it was at the first time and replay the music.
I am trying a Boolean. When it's true, the music start and when it's false the music, but it doesn't work!
In addition, how I can make another activity play or stop the music depending on the main activity?
public class MainActivity extends AppCompatActivity {
MediaPlayer mp;
ImageButton SoundButton;
ImageButton NoSoundButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SoundButton = new ImageButton(this);
NoSoundButton = new ImageButton(this);
/*---------Image Buttons--------*/
SoundButton=(ImageButton) findViewById(R.id.sound);
SoundButton.setVisibility(View.GONE);
NoSoundButton=(ImageButton) findViewById(R.id.nosound);
NoSoundButton.setVisibility(View.VISIBLE);
/*---------Media Player--------*/
mp = new MediaPlayer();
mp = MediaPlayer.create(this, R.raw.aud);
mp.setLooping(true);
mp.start();
}
public void nosound(View view) {
SoundButton.setVisibility(View.VISIBLE);
NoSoundButton.setVisibility(View.INVISIBLE);
mp.stop();
mp.prepareAsync();
}
public void sound(View view) {
SoundButton.setVisibility(View.INVISIBLE);
NoSoundButton.setVisibility(View.VISIBLE);
mp.start();
}
}