I have four buttons of my main menu for my android game. In my option, there's on and off. When the user clicks the on button, the music should play when I start to click play. And if the user clicks the off, the music will not play. But the default is, the music will play.
I tried to code on my own, if the user clicks the on
button, there's this sign
variable will set to 1 and this value will pass to the play activity. Where if the sign value is equivalent to 1, the music will start; if 0, the music will not play. But, after doing this, after I click off, it results to "FORCE CLOSE
" of my game.
I have this code for my Options activity:
musicOn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent put = new Intent(getApplicationContext(), Level1.class);
musicSwitch = 1;
Bundle cargo = new Bundle();
cargo.putInt("sign", musicSwitch);
put.putExtras(cargo);
}
});
musicOff.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent put = new Intent(getApplicationContext(), Level1.class);
musicSwitch = 0;
Bundle cargo = new Bundle();
cargo.putInt("sign", musicSwitch);
put.putExtras(cargo);
}
});
And this one for my level1 activity:
int value=1;
Bundle cargo = getIntent().getExtras();
value = cargo.getInt("sign");
if(value==1){
MediaPlayer mPlayer = MediaPlayer.create(Level1.this, R.raw.sounds);
mPlayer.start();
mPlayer.setLooping(true);
}