Search code examples
javaandroidloopsaudiolibgdx

Why does my 30-second sound plays only 4 seconds in my LibGDX project?


I tried to set a backgroundsound in my LibGDX project. I wrote in the Asset.java file following code:

public static void playBackgroundsound(){
     backgroundsound.loop();
}

After this I went in the MainMenuScreen.java file and and wrote this line in the onClick() of the start Button from the game:

Assets.playBackgroundsound();

The sound is 30 seconds long and should only loop after its played completely. The problem is that it loops after 4 seconds. What is the Reason for it and how can I solve this problem??

Here is the GitHub project: https://github.com/DaFaack/FlappyBibi


Solution

  • background1.mp3 may be of 30 sec not backgroundsound.mp3 check your assets folder.


    Mp3 files have some decoding delay at the start.

    You can use .wav file instead of .mp3 for less delay also for Sound Effect you should use small .mp3 file.

    In my suggestion for background sound, you should use Music instead of Sound.

    According to wiki :

    Sound effects are small audio samples, usually no longer than a few seconds, that are played back on specific game events such as a character jumping or shooting a gun.

    Music is -

    For any sound that's longer than a few seconds it is preferable to stream it from disk instead of fully loading it into RAM. Libgdx provides a Music interface that lets you do that.

    Loading and playing music is as simple as Sound :

    Music music = Gdx.audio.newMusic(Gdx.files.internal("data/mymusic.mp3"));
    music.setLooping(true);
    music.play();