2
\$\begingroup\$

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?

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

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .