简体   繁体   中英

Java heap error when loading Sounds using Slick2D

I am writing a game using LWJGL and Slick2D. I just recently got into a place that I can't get out of. I have tried to load and play a OGG audio file that is 3.2MB in size (if this counts, the file was originally MP3, but because of the licensing issues with MP3, I chose to use OGG instead. I used this converter to convert my MP3 to an OGG). My environment is Eclipse (latest ADT, but still works for standard Java development), Mac OSX 10.9 Mavericks. Here is my problem.

I load my game with a set of 45 FPS, although that shouldn't matter since the Slick2D audio loading thread is independent. In my game's initialization (from Slick2D's BasicGame class), to test the loading assets part of Slick2D, I load, then directly play a the 3.2MB OGG sound:

@Override
public void Initialize(GameContainer _gameContainer)
{
    org.newdawn.slick.Sound _testSound = null;
        _testSound = new org.newdawn.slick.Sound("res/Sounds/Menu_Theme.ogg");
    } catch (SlickException e) {
        e.printStackTrace();
    }
    _testSound.play();

}

When I run my game, I get a "Exception in thread 'main' java.lang.OutOfMemoryError: Java heap space" exception. I have tried expanding my JVM memory limit to 2048MB, but still had the same exception. Note that I have also tried not playing the sound directly after loading; still had the exception (big surprise). However, when I tried loading a different OGG sound file (this file had gone through the same converter as before, but was much smaller: 6KB), the sound file loaded and played correctly. This makes me think that I am inefficiently loading my 3.5MB file?

Here is the full console output from my program (with the larger 3.5MB audio file):

Sat Dec 07 23:15:01 PST 2013 INFO:Slick Build #237
Sat Dec 07 23:15:01 PST 2013 INFO:LWJGL Version: 2.9.0
Sat Dec 07 23:15:01 PST 2013 INFO:OriginalDisplayMode: 1280 x 800 x 32 @0Hz
Sat Dec 07 23:15:01 PST 2013 INFO:TargetDisplayMode: 1280 x 800 x 0 @0Hz
Sat Dec 07 23:15:01 PST 2013 INFO:Starting display 1280x800
Sat Dec 07 23:15:01 PST 2013 INFO:Use Java PNG Loader = true
Loading: net.java.games.input.OSXEnvironmentPlugin
Sat Dec 07 23:15:02 PST 2013 INFO:Found 0 controllers
Sat Dec 07 23:15:02 PST 2013 INFO:Initialising sounds..
Sat Dec 07 23:15:02 PST 2013 INFO:- Sound works
Sat Dec 07 23:15:02 PST 2013 INFO:- 64 OpenAL source available
Sat Dec 07 23:15:02 PST 2013 INFO:- Sounds source generated
AL lib: (WW) FreeContext: (0x7ff50f813780) Deleting 64 Source(s)
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2786)
    at java.io.ByteArrayOutputStream.toByteArray(ByteArrayOutputStream.java:133)
    at org.newdawn.slick.openal.OggDecoder.getData(OggDecoder.java:322)
    at org.newdawn.slick.openal.SoundStore.getOgg(SoundStore.java:835)
    at org.newdawn.slick.openal.SoundStore.getOgg(SoundStore.java:793)
    at org.newdawn.slick.Sound.<init>(Sound.java:87)
    at classicsevolved.Main.Initialize(Main.java:31)
    at com.zerocoolamusement.FaithfulFountain.Game.init(Game.java:45)
    at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:393)
    at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:317)
    at classicsevolved.Main.main(Main.java:65)

So, my question is how can I load audio files more efficiently? Is there a better framework to Slick2D to load and play sound effects in my game? (I don't know OpenAL, but I know a tad of OpenGl. Just a tad, enough to make a Quad and a Line :-D).

Oh, and I guess that I should mention that my Game does nothing else EXCEPT load and play that audio file.

I got it now. I BELIEVE that the problem was that Slick2D might have been decompressing the OGG AS it was reading it from the file stream, using up all of its memory like it was nothing. I simply converted my original MP3 into a WAV, and put it into my project. Worked perfectly. It also loaded the audio file quite quickly, and I would assume because it doesn't have to decompress anything. I also tried to convert the MP3 into an AIFF-C, which still worked (unsurprisingly).

The good news: my audio files work, and load faster. The bad news: I have to trade functionality for an uncompressed audio type, making my audio files 30 times larger than the original MP3. But, I personally don't care about size, I care about functionality.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM