Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play music in loop in libgdx?

I created the music in on create like this:

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

the problem that its not playing in loop.

I also tried without the loop and instead registering for setOnCompletionListener but it also doesn't play. when I tried to reload the file like this:
music_background = Gdx.audio.newMusic(Gdx.files.internal("background_music.mp3")); Inside the event it worked but only one time.

I think that the problem is that when its done playing the file dispose itself...

How can I play music in loop? what I'm doing wrong?

like image 391
Alex Kapustian Avatar asked Jan 04 '15 15:01

Alex Kapustian


1 Answers

You are doing it correct, but MP3s are not good for looping, use OGG instead. MP3s will add a short silence at the start, OGG or WAV doesn't have this limitation.

Here is my code that works perfectly:

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 you MP3s, edit away the blank audio and export as OGG.

like image 78
karl Avatar answered Nov 15 '22 04:11

karl