4

I'm trying to create a music based game (similar to "my singing monsters). the idea is that I have a scene where I can place and remove a few different characters from the scene, each character has a unique mp3 file for its singing.

I'm building my game with cocos2dx though for sounds I'm using a custom engine which currently uses MediaPlayer API on android. In order to sync all the sounds together I just play them all when the scene is loaded and then just mute and unmute according to which character is on scene. the problem is that on older devices the sounds seems to be out of sync, I assume that I need a way to pre-cache the mp3 files before they are loaded.

I tried creating all the mediaPlayers in advanced and use the prepare() method, then save them in a hashmap and just use play() when the scene is started to play them all. unfortunately it didn't work and the tracks are still out of sync. I'm not sure how MediaPlayer work so I don't know if trying to create it in advanced is equivalent to pre-caching. The standard cocos2dx sounds engine uses soundpool which I understand is not good for large files and I'm not sure if it has a caching mechanism. On Ios I already had a sounds engine that uses openAL and on the ipad 4 I tested it works just fine.

What do you suggest I do in order to sync the tracks? implement another sounds engine? maybe based on openSL? or somehow keep a timer and use it to sync tracks.

How would design the sound for such a game where it is the most important thing to have all the tracks synced together?

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
Amit Ofer
  • 413
  • 1
  • 7
  • 26
  • 1
    crossposting is discouraged: http://gamedev.stackexchange.com/questions/61554/syncing-multiple-audio-tracks-on-android – Bjorn Roche Aug 31 '13 at 17:37

1 Answers1

1

The problem is that each MediaPlayer represents a separate stream, which may start at different times. Even if you call start on all of them at the same time (which is impossible), there's no way to guarantee that the OS will load and actually start each one at the same time.

As far as I know, Android offers no way to synchronize multiple streams. Even with operating systems that do offer this capability, it's extremely cumbersome, and usually not 100% accurate.

The correct solution is to open a single stream, eg using the audio track interface, and do the MP3 decoding and mixing yourself before sending the data to the stream.

Bjorn Roche
  • 11,279
  • 6
  • 36
  • 58
  • Anyone has a good tutorial on how to achieve this? I think the better option is to somehow do it in native code since I will need to do the same thing for IOS as well, but seperate code is also ok. – Amit Ofer Sep 01 '13 at 07:11
  • Alas, I've never seen anything like that (which is not to say it doesn't exist). This might be helpful: http://blog.bjornroche.com/2013/05/the-abcs-of-pcm-uncompressed-digital.html – Bjorn Roche Sep 01 '13 at 15:08