OpenAL Error

Hi,



in my ingame-gamestate i setup the audiosystem, add tracks to the queue and play them. This works fine so far.

Yet, when i go back to my menu and start a new game (this detaches the old ingame-state and creates a new one), i get an OpenAL Error during the audio update phase.





Here's how i set up the audiosystem (called in the ingame-state constructor):


private void setupAudio() {
      // grab a handle to the audio system
      audio = AudioSystem.getSystem();

      // setup our ear tracker to track the camera's position and orientation.
      audio.getEar().trackOrientation(cam);
      audio.getEar().trackPosition(cam);

      // setup a music score for our demo
      AudioTrack music1 = getMusic(IngameState.class.getResource("/data/sound/Ambient_ Swelling_Pad.wav"));
      audio.getMusicQueue().setRepeatType(RepeatType.ALL);
      audio.getMusicQueue().setCrossfadeinTime(2.5f);
      audio.getMusicQueue().setCrossfadeoutTime(2.5f);
      audio.getMusicQueue().addTrack(music1);
      audio.getMusicQueue().play();
   }





This is how i make a new game from the menu:


private class EnterAction extends InputAction {
      public void performAction(InputActionEvent evt) {
         GameStateManager.getInstance().detachChild("ingame");
         GameState ingame = new IngameState(params);
         ingame.setActive(true);
         GameStateManager.getInstance().attachChild(ingame);
         myState.setActive(false); // Deactivate this (the menu) state.
      }
   }





And this is the error that occurrs when i call the enter action a second time:


11.09.2009 11:28:38 com.jmex.audio.openal.OpenALStreamedAudioPlayer playInNewThread
SCHWERWIEGEND: Audio Error!
org.lwjgl.openal.OpenALException: OpenAL error: Invalid Value (40963)
   at org.lwjgl.openal.Util.checkALError(Util.java:64)
   at org.lwjgl.openal.AL10.alBufferData(AL10.java:1059)
   at com.jmex.audio.openal.OpenALStreamedAudioPlayer.stream(OpenALStreamedAudioPlayer.java:319)
   at com.jmex.audio.openal.OpenALStreamedAudioPlayer.playStream(OpenALStreamedAudioPlayer.java:190)
   at com.jmex.audio.openal.OpenALStreamedAudioPlayer.playInNewThread(OpenALStreamedAudioPlayer.java:214)
   at com.jmex.audio.openal.OpenALStreamedAudioPlayer.play(OpenALStreamedAudioPlayer.java:166)
   at com.jmex.audio.AudioTrack.play(AudioTrack.java:117)
   at com.jmex.audio.MusicTrackQueue.play(MusicTrackQueue.java:157)
   at com.jmex.audio.MusicTrackQueue.setCurrentTrack(MusicTrackQueue.java:225)
   at com.jmex.audio.MusicTrackQueue.update(MusicTrackQueue.java:253)
   at com.jmex.audio.openal.OpenALSystem.update(OpenALSystem.java:126)
   at gamestates.IngameState.stateUpdate(IngameState.java:193)
   at com.jmex.game.state.CameraGameStateDefaultCamera.update(CameraGameStateDefaultCamera.java:90)
   at com.jmex.game.state.GameStateNode.update(GameStateNode.java:71)
   at main.Connections.update(Connections.java:51)
   at com.jme.app.BaseGame.start(BaseGame.java:84)
   at main.Connections.main(Connections.java:164)




I hope someone can help me with this :)

One potential problem I saw - have you tried using class.getClassLoader() instead?  Change this:

AudioTrack music1 = getMusic(IngameState.class.getResource("/data/sound/Ambient_ Swelling_Pad.wav"));



To this:

AudioTrack music1 = getMusic(IngameState.class.getClassLoader().getResource("/data/sound/Ambient_ Swelling_Pad.wav"));



I tend to have null pointer problems when I try to do it the other way.

getClassLoader doesnt work at all.

If i use that, i get a nullpointer-exception when trying to load the music file.

But i guess the class / getClassLoader isn't the problem anyways, since the music plays fine when i start the game.



Only when i go back to the menu:



IngameState.setActive(false);

GameStateManager.getInstance().getChild("menu").setActive(true);



and after that detach the ingamestate and make a new one ( using the above posted enterAction), the error occurs.

I think, when i detach the ingamestate, then the audiosystem might not get "cleaned up" ? and therefore when i make a (second) new gamestate the audiosystem screws up?


Well, i found a solution, but im not too happy with it:



I changed the EnterAction in my menu like this:


private class EnterAction extends InputAction {
      public void performAction(InputActionEvent evt) {
         AudioSystem.getSystem().cleanup();
         if(GameStateManager.getInstance().getChild("ingame") != null){
            GameStateManager.getInstance().getChild("ingame").cleanup(); // terminate threads inside
            GameStateManager.getInstance().detachChild("ingame");
         }
         GameState ingame = new IngameState(params);
         ingame.setActive(true);
         GameStateManager.getInstance().attachChild(ingame);
         myState.setActive(false); // Deactivate this (the menu) state.
      }
   }



What i dont like about this is that i always have to separately call AudioSystem.cleanup() and even ingamestate.cleanup() instead of just detaching the gamestate :(
Well, if i integrate the audiosystem.cleanup into my ingame.cleanup(), i only have to call one cleanup() method from the menu.

Isn't there a better way to do this?