Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8024629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:54:59+00:00 2026-06-04T22:54:59+00:00

I’ve created an app which starts with a main Activity. On that main Activity,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:55:01+00:00Added an answer on June 4, 2026 at 10:55 pm

    Inside the MENU_MUSIC and MENU_SOUND handling you have the following lines of code:

    if (!mPlayMusic) {
        mPlayMusic=true;
        ... playMusic(); ...
    }
    if(mPlayMusic) {
        mPlayMusic=false;
        ... stopMusic(); ...
    }
    

    Since you omitted the else whenever the first if condition is true also the second one is triggered (due to the assignment mPlayMusic=true; inside the first) and thus the music immediately stops after starting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.