Playing two tracks at the same time

Hi there :slight_smile:



I have some background music playing in my app, which plays fine. I also have a sound effect that I want to play to sort of layer the sounds. They both work fine individually, but when I try and get them to both play at the same time, jME gives me this error:



SEVERE: Exception in game loop

java.lang.OutOfMemoryError: Direct buffer memory
   at java.nio.Bits.reserveMemory(Bits.java:633)
   at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:95)
   at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:288)
   at org.lwjgl.BufferUtils.createByteBuffer(BufferUtils.java:60)
   at com.jme.scene.state.lwjgl.LWJGLTextureState.load(LWJGLTextureState.java:328)
   at com.jme.scene.state.lwjgl.LWJGLTextureState.apply(LWJGLTextureState.java:465)
   at com.jme.renderer.lwjgl.LWJGLRenderer.applyStates(LWJGLRenderer.java:1609)
   at com.jme.renderer.lwjgl.LWJGLRenderer.draw(LWJGLRenderer.java:955)
   at com.jme.scene.batch.TriangleBatch.draw(TriangleBatch.java:255)
   at com.jme.renderer.RenderQueue.renderTransparentBucket(RenderQueue.java:275)
   at com.jme.renderer.RenderQueue.renderBuckets(RenderQueue.java:239)
   at com.jme.renderer.Renderer.renderQueue(Renderer.java:452)
   at com.jme.renderer.lwjgl.LWJGLRenderer.displayBackBuffer(LWJGLRenderer.java:509)
   at com.jme.app.BaseGame.start(BaseGame.java:85)
   at assignment.MainGame.main(MainGame.java:153)
21-Jan-2008 16:06:31 com.jme.app.BaseGame start
INFO: Application ending.



The code that i am using is:


//Main background music
        String tracksDir = "assignment/";
      
      List<String> tracks = new ArrayList<String>();
      tracks.add("ogg10.ogg");
      tracks.add("ogg11.ogg");
      
      
      
      
   
      try {
         ResourceLocatorTool.addResourceLocator(
            ResourceLocatorTool.TYPE_AUDIO,
            new SimpleResourceLocator(TestMusicQueue.class
                   .getClassLoader().getResource(tracksDir + tracks.get(0))));
      } catch (URISyntaxException e) {
         e.printStackTrace();
      }
      
      AudioSystem audio = AudioSystem.getSystem();
      
      final List<AudioTrack> loaded = new ArrayList<AudioTrack>();
      
      for (String track : tracks) {
         AudioTrack at = audio.createAudioTrack(track, true);
           at.setType(TrackType.MUSIC);
           at.setRelative(true);
           at.setTargetVolume(0.7f);
           at.setLooping(false);
          
           loaded.add(at);
      }
      Collections.shuffle(loaded);
       
      

      for (AudioTrack at : loaded) {
         audio.getMusicQueue().addTrack(at);
      }

      
        audio.getMusicQueue().setRepeatType(RepeatType.ALL);
        audio.getMusicQueue().setCrossfadeinTime(2.5f);
        audio.getMusicQueue().setCrossfadeoutTime(2.5f);
        audio.getMusicQueue().play();
       
        //End of main background music

//New Audio Stuff
       
      
      List<String> tracks2 = new ArrayList<String>();
      tracks2.add("another.ogg");
      
      
      
      
   
      try {
         ResourceLocatorTool.addResourceLocator(
            ResourceLocatorTool.TYPE_AUDIO,
            new SimpleResourceLocator(TestMusicQueue.class
                   .getClassLoader().getResource(tracksDir2 + tracks2.get(0))));
      } catch (URISyntaxException e) {
         e.printStackTrace();
      }
        AudioSystem audio2 = AudioSystem.getSystem();
      
      final List<AudioTrack> loaded2 = new ArrayList<AudioTrack>();
      
      for (String track2 : tracks2) {
         AudioTrack at2 = audio2.createAudioTrack(track2, true);
           at2.setType(TrackType.MUSIC);
           at2.setRelative(true);
           at2.setTargetVolume(0.7f);
           at2.setLooping(false);
          
           loaded2.add(at2);
      }
   
       
   

      for (AudioTrack at2 : loaded2) {
         audio2.getMusicQueue().addTrack(at2);
      }

      
        audio2.getMusicQueue().setRepeatType(RepeatType.ALL);
        audio2.getMusicQueue().setCrossfadeinTime(2.5f);
        audio2.getMusicQueue().setCrossfadeoutTime(2.5f);
        audio2.getMusicQueue().play();
        //End of audio stuff



Is this a common problem, or is there something that I can do to ensure it doesn't get the out of memory message?

Many thanks for your time :)

Oh right - hang on, you can only have one audio system - that would be why things are messing up… In that case - can you add two different sets of tracks into one audio system and it'll play em simultaneously?



Thanks :slight_smile:

I believe the virtual machine limits the amount of memory the program can use, and since sound files can be quite large there is only enough memory to keep one at a time in memory.  Try adding the command line argument -Xmx 1024 to increase the heap size.  You could also set the longer sounds to stream mode.

Direct buffers are not the same as Heap memory, and there is another VM argument that you need for that…

http://www.jmonkeyengine.com/jmeforum/index.php?topic=5822.0

Is it possible to decode the audio file while it is being streamed? I think this would solve the memory problem quite nicely.