Search code examples
audiolibgdxmutenormalize

Muting 'Sound' instead of 'Music' in libgdx


I'm developing a simple game using eclipse and libgdx. Currently, I'm using 'Music' instead of 'Sound' for sound effects for my game. I made a button for muting all of the sound fx but having a problem when it comes to 'sound' instead of music.

Here's my current code:

public static Music jump;

public static void load() {
jump = Gdx.audio.newMusic(Gdx.files.internal("data/jump.wav"));
}

public static void muteFX() {
lesgo.setVolume(0);

public static void normalizeFX() {
jump.setVolume(1f);


//'muteFX' and 'normalizeFX' to be called on different class

I wanted to change it to 'Sound' (the reason is I wanted it to be more responsive on fast clicks,) So it could be like this:

public static Sound jump;

public static void load() {
jump = Gdx.audio.newSound(Gdx.files.internal("data/jump.wav"));
}

/* here comes my problem, didn't know how to set mute and normal
volume for the jump sound. I know there is also a set volume method
to 'Sound' but really confused on the terms (long soundID, float volume)
Can someone make this clear to me on how to implement the long and soundID?
*/

I'm really new to libgdx, as well as in java. I researched many forums regarding this and still can't find a more clearer explanation. Any help will be greatly appreciated.

Thanks a lot in advance! =)


Solution

  • A quick suggestion would be to use a global variable for sound

    public static VOLUME = 1.0f;
    

    The sound api allows you to play a sound at a certain volume, so what you could do is have all the sounds in your game play at that global value when needed.

    jump.play(VOLUME);
    

    this way, all your switch will do is change the float value of volume.

    public static void muteFX(){
        VOLUME = 0.0f;
    }
    public static void normalizeFX(){
        VOLUME = 1.0f;
    }
    

    It would be in your best interest to not use the Music class for sound effects due to memory constraints.

    I hope this helps