8

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?

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
Alex K
  • 5,092
  • 15
  • 50
  • 77
  • 1
    The method name is `setLooping` not `SetLoop`. Also you can try methods `isLooping()` and `getPosition()` after music ending for debug. – Rara Jan 04 '15 at 17:03
  • yes I wrote it myself here and didn't copy it from my code. I tried isLooping() and getPosition(). The first methond is true and the second is 0.. what can I do with it? – Alex K Jan 04 '15 at 18:29
  • I tried your method of `Music` asset loading in my app. It works and loops ok. I also tried to get `isLooping()` and `getPosition()` values with `OnCompletionListener` to compare with yours but method didn't called when music starts again. Probably it is called only when music stops which is never happens in my case. A couple more thoughts: 1. Can you try some other mp3's? May be your current one is corrupted? 2. Is your `music_background` field declared as `static`? – Rara Jan 05 '15 at 16:11
  • One more question. On what backends do you run your app for tests? – Rara Jan 05 '15 at 16:34
  • I tried another mp3 file and it didn't work well (the loop doesn't work). you are correct about OnCompletionListener it works only when the loop is set to false. music_background isn't static. I run it on Android. Can you give me the file that you tested with? – Alex K Jan 05 '15 at 18:53
  • Here is my [mp3](https://www.dropbox.com/sh/bhd49a2x7v67tox/AADVzBV_lE3du64thFL92aLca?dl=0). I rechecked it on Android, iOS and Linux. Everywhere it works for me. Try to write separate simple app with just music loading, setting looping and playing. May be it will help to localize the problem. Also you could test on other backends. – Rara Jan 06 '15 at 09:55

1 Answers1

12

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.

Michiel Pater
  • 22,377
  • 5
  • 43
  • 57
karl
  • 1,766
  • 2
  • 13
  • 24