How to play music in a loop in libgdx?

I created music in create mode as follows:

music_background = Gdx.audio.newMusic(Gdx.files.internal("background_music.mp3")); music_background.setLooping(true); 

the problem is that he does not play in the loop.

I also tried without a loop and instead registered for setOnCompletionListener , but it also fails to play. when I tried to reload the file as follows:
music_background = Gdx.audio.newMusic(Gdx.files.internal("background_music.mp3")); Inside this event, he worked, but only once.

I think the problem is that when its execution in the file will be destroyed ...

How can I play music in a loop? what am I doing wrong?

+5
source share
1 answer

You are doing it right, but MP3s are not suitable for looping, use OGG instead. MP3s will add a short silence at the beginning, OGG or WAV do not have this limitation.

Here is my code that works great:

 menuMusic = Gdx.audio.newMusic(Gdx.files.internal("data/sounds/music_menu.ogg"); menuMusic.setLooping(true); menuMusic.play(); 

If you have all your files in MP3, just download Audacity, import MP3 files, edit the blank sound and export it as OGG.

+6
source

Source: https://habr.com/ru/post/1210360/


All Articles