I am currently developing a game with Java, with a soundtrack consisting of ten tracks in .ogg format, 1-2 MB in size each. To be able to switch between these tracks quickly and easily in the game, before the game starts, I load all of them into memory by creating ten Clip classes. Unfortunately, but understandably, pre-loading these tracks increases the application's CPU usage from about 61 MB to 340 MB.

Since I'm only ever playing one track at a time, I could make it so that only one Clip is ever in memory, and is destroyed when the track needs to change. This means, though, that I'll have to hang the game up on loading the Clip when this happens, or do this on a different thread during a few seconds of undesirable silence.

Is there a smarter, more conventional way to handle large BGM files, or do I just have to pick my poison?

  • \$\begingroup\$ Could you stream the tracks from the disk instead of loading them to the memory? Google for OggInputStream. \$\endgroup\$ – msell Nov 3 '15 at 13:57
  • \$\begingroup\$ Am I able to pause an OggInputStream without delay as I am with Clips? \$\endgroup\$ – Craig Horwood Nov 3 '15 at 23:06

The easiest method is to only keep 2 tracks in memory; one playing and one ready to be played.

When one track is done you start the next one and also start loading the track (in a background thread) to be played after that one and close() the Clip that just finished.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.