I’ve created an app which starts with a main Activity. On that main Activity, there are 2 checkboxes (cbPlayMusic and cbPlaySound). When you start the game by clicking the start button,with this code:
btStart.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(GobbleFishMainActivity.this, GobbleFishGame.class);
intent.putExtra(INTENT_EXTRA_PLAY_MUSIC, cbMusic.isChecked());
intent.putExtra(INTENT_EXTRA_PLAY_SOUND, cbSound.isChecked());
startActivity(intent);
}
});
On top of the activity is this:
public static final String INTENT_EXTRA_PLAY_MUSIC = "be.fielibert.gobblefish.extra.PLAY_MUSIC";
public static final String INTENT_EXTRA_PLAY_SOUND = "be.fielibert.gobblefish.extra.PLAY_SOUND";
Then when you’ve clicked Start, GobbleFishGame Activity, this is the onCreate code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initBitmaps();
mGraphView = new GraphView(this);
mPlayMusic = getIntent().getBooleanExtra(GobbleFishMainActivity.INTENT_EXTRA_PLAY_MUSIC,true);
mPlaySound = getIntent().getBooleanExtra(GobbleFishMainActivity.INTENT_EXTRA_PLAY_SOUND,true);
setContentView(mGraphView);
mGraphView.runProgram();
playMusic();
}
When mPlayMusic is true, the background music starts playing when the GobbleFishGame Activity is created due to playMusic():
private void playMusic(){
if (!mPlayMusic) {
return;
}
mPlayer = MediaPlayer.create(GobbleFishGame.this, R.raw.song);
mPlayer.setLooping(true);
mPlayer.start();
}
Everything works fine until now. But, when you press the menu button on your Android device, a menu is created with 4 options: Main menu, Restart game, Toggle music, Toggle sound.
This is the code for the menu:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE, MENU_MENU, Menu.NONE, "Main menu");
menu.add(Menu.NONE, MENU_RESTART, Menu.NONE, "Restart game");
menu.add(Menu.NONE, MENU_MUSIC, Menu.NONE, "Toggle music");
menu.add(Menu.NONE, MENU_SOUND, Menu.NONE, "Toggle sound");
return true;
}
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
mGraphView.pauseGame();
return super.onMenuOpened(featureId, menu);
}
@Override
public void onOptionsMenuClosed(Menu menu) {
super.onOptionsMenuClosed(menu);
if(isStopping=true){
return;
}
else{
mGraphView.continueGame();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == MENU_MENU) {
//mPlayer.release();
isStopping=true;
stopMusic();
mGraphView.mState = STATE_STOP;
Intent intent = new Intent(GobbleFishGame.this, GobbleFishMainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
//finish();
}
else if (item.getItemId() == MENU_RESTART) {
mGraphView.init();
mPlayer.reset();
playMusic();
}
else if (item.getItemId() == MENU_MUSIC) {
if (!mPlayMusic) {
mPlayMusic=true;
if (mPlayer != null) {
//mPlayer.start();
playMusic();
}
//playMusic();
}
if(mPlayMusic){
mPlayMusic=false;
stopMusic();
}
}
else if (item.getItemId() == MENU_SOUND) {
if (!mPlaySound) {
mPlaySound=true;
if (mp != null) {
mp.reset();
}
//mp.reset();
}
if(mPlaySound){
mPlaySound=false;
}
}
return true;
}
However, the options Toggle music and Toggle sound won’t work. This is what happens: when checkbox music is true (so the music is playing), if you then toggle music it works fine. But, if you then want to toggle music back on, it works too, but then the pause doesn’t work anymore. In pause I call mPlayer.pause, and that works fine if I haven’t toggled music on or off. If I have toggled music on or off, pause doesn’t work anymore for some reason.
This is pause code:
public void pauseGame() {
if (mMode != MODE_PAUSE) {
mMode = MODE_PAUSE;
if (!mPlayMusic) {
return;
}
if (mPlayer != null) {
mPlayer.pause();
}
}
}
So that’s my problem. The same problem occurs with toggle sound: if I toggle sound off, it works and there is no sound anymore (sounds like when my fish eats another fish or game over sound), but when I toggle sound on again, it doesn’t work: no sound is played when it should be played.
My guess is that it has something to do with the booleans. I don’t know what’s wrong with my code though, what am I doing wrong?
Thanks in advance.
Inside the
MENU_MUSICandMENU_SOUNDhandling you have the following lines of code:Since you omitted the
elsewhenever the firstifcondition is true also the second one is triggered (due to the assignmentmPlayMusic=true;inside the first) and thus the music immediately stops after starting.