Libgdx / Java - Synchronizing game with music?

I've seen a lot of people have problems with this but haven't found a decent solution yet. There are some rhythm elements in my game, so I need some events to happen every time, for example. Initially I just used the music "getPosition ()" to get a nice timer that works great on Windows but not Android:

  • OGG and MP3 files seem to give inconsistent readings, the "getPosition ()" value doesn't seem to be updated as often as it does on the desktop
  • M4A files seem to give consistent readings, but with a delay that varies from device to device. Maybe a last resort if I included the setting in the game to adjust the latency, but this kind of sucks for casual play.
  • WAV files seem to give the best readings, but the file size makes them nearly impossible to use.

So, I've given up on using getPosition () and am trying to create my own timer instead. I've tried two methods:

  • Adding delta time until I get 60 / bpm (time between each bit)
  • Using recursive method with Timer.schedule (new task ()) 60 / bpm seconds.

But both of these methods seemed to have the same result: they seem to work at first, but the time is off a bit, so it gets worse over time.

The only thing that has been able to sync so far is the twin events from the Universal Tween Engine. If I have a repeating animation that runs at 60 / bpm, no delay, it seems to sync perfectly with the music. So any generic Tween Engine used to keep time should be what I'm looking for.

+3


source to share


1 answer


This problem seems to be especially for Android.

The getPosition () call in libgdx (in backends / gdx-backend-android / src / com / badlogic / gdx / backends / android / AndroidMusic.java) is called via Android MediaPlayer.getCurrentPosition ().



This method has known problems with audio offsets and also uses only millisecond resolution. See: https://code.google.com/p/android/issues/detail?id=11590

I would recommend using a different nanosecond precision timer routine for this particular problem.

0


source







All Articles