StandardGame and Play-Once Environmental Sounds

All right, it's time for me to address some music and sound effect ideas.  What I want is a fairly standard setup: looping background music and sound effects on top of that which play only once per condition to execute them (examples: menu bonks, explosions, the screams of the dying, etc.).  None of this needs to be positional to models in the scene and the camera. 



So far, I have been successful with the looping background music; however the play-once sound effects (currently in the lab set up to fire off by a keystroke) are looping and each successive execution overlays on top of the previous ones.  The only way that I have yet found to get the behavior I want is to grab the environment sound pool on each update and check if the current environmental sounds are equal to or greater than their max time and then to remove them from the pool if that is true.  Somehow, this just doesn't feel right.  Here is the code I am using to execute background music and a play-once sound (modified, of course, from TestJmexAudio.java test):




   private void createSFXAudio(){

      // loaded at game/lab startup

      System.out.println("createSFXAudio()...");

      AudioTrack audBgMusic = AudioSystem.getSystem().createAudioTrack(
            WorldTwoGS.class.getClassLoader().getResource("assets/audio/bgmusic-test.wav"), false);
      AudioSystem.getSystem().getMusicQueue().setRepeatType(RepeatType.ONE);
      audBgMusic.setType(TrackType.MUSIC);
      audBgMusic.setRelative(false);
      audBgMusic.setLooping(true);
      AudioSystem.getSystem().getMusicQueue().addTrack(audBgMusic);
      AudioSystem.getSystem().getMusicQueue().play();

   }

   private AudioTrack getSFX(URL resource) {
      
      AudioTrack sound = AudioSystem.getSystem().createAudioTrack(resource, false);
      sound.setType(TrackType.ENVIRONMENT);
      sound.setRelative(false);
      sound.setLooping(false);
      return sound;
   }
   
   private void executeSFXAudio(){

      // called by keystroke

      System.out.println("executeSFXAudio()...");
      
      AudioTrack audHeal = getSFX(WorldTwoGS.class.getClassLoader().getResource("assets/audio/healogg.ogg"));
      AudioSystem.getSystem().getEnvironmentalPool().addTrack(audHeal);
      
   }

   private void checkAudio(){
      
      // called from update()
      
      ArrayList<AudioTrack> alAudio = AudioSystem.getSystem().getEnvironmentalPool().getTrackList();
      
      if (alAudio.size() > 0){

         System.out.println("checkAudio()... alAudio.size() > 0");
         
         for (int i = 0; i < alAudio.size(); i++){

            System.out.println("alAudio[" + i + "] isActive: " + alAudio.get(i).isActive() );
            System.out.println("alAudio[" + i + "] getCurrentTime: " + alAudio.get(i).getCurrentTime());
            System.out.println("alAudio[" + i + "] getTotalTime: " + alAudio.get(i).getTotalTime());
            System.out.println("alAudio[" + i + "] isLooping: " + alAudio.get(i).isLooping());
            System.out.println("alAudio[" + i + "] getType: " + alAudio.get(i).getType());

            if (alAudio.get(i).getType().compareTo(TrackType.ENVIRONMENT) == 0){
               if (alAudio.get(i).getCurrentTime() >= alAudio.get(i).getTotalTime()){
                  System.out.println("!!!!REMOVE SFX AUDIO TRACK!!!!!");
                  AudioSystem.getSystem().getEnvironmentalPool().removeTrack(alAudio.get(i));

               }
            }

         }
      }
      
   }



Is there a better, more proper, way to handle this?


Use HEADSPACE or POSITIONAL type for play-once sounds. Environmental sounds are looped.


  
    public enum TrackType {
        /** Sound track **/
        MUSIC,
        /** non-positional sound, generally ambient and looped **/
        ENVIRONMENT,
        /** 3d positional sound effect **/
        POSITIONAL,
        /** non-positional sound effect **/
        HEADSPACE
    }