Search code examples
javabuttonaudiolibgdxpreferences

Saving Sound Preference Libgdx Android


Trying to make an image for sound control. Need to make an image turn music on and off. And save it so that the music doesn't restart every time you go back to the main menu. Trying to use preferences any help would be appreciated.

private Preferences prefs;
private boolean spref = true;


prefs = Gdx.app.getPreferences("Soundpref");
prefs.putBoolean("soundOn", true);
intro = Gdx.audio.newMusic(Gdx.files.internal("Sound/intro.wav"));

  if (prefs.getBoolean("Sound", true))
    {
        intro.play();
    }

and this is my image listener

   if (menuPanel.spressed && spref == true) {
       intro.stop();
       prefs.flush();
       prefs.putBoolean("Sound", false);
       spref = false;
   }

Solution

  • The problem is with different names in prefs: soundOn and Sound. The same variable should be used.

    Also notice that prefs.getBoolean("variable", true) would return true if that variable doesn't exist.