Audio stops working after stopping and starting AudioTrack


Hi,

I'm having a problem with an AudioManager class I have written. It has pretty simple functionality. It basically has a HashMap of AudioTracks, and when I call play("key") it stops the current track and plays the new one. It also has a fadeout function that fades the master gain.

My problem is that after I have swapped sounds a certain number of times (seems to be quite consistently around 30-32 times) the whole audio becomes silent. I've put in a bunch of  conditionals to try to catch the problem, but nothing has helped so far.

I guess my main question is whether there is some known reason why the system would fail just because I'm stopping and starting tracks. I'm wondering if it may be thread related. I'm calling play() from within the game update function.

Advice from the jme audio experts would be much appreciated!

This is my class, as it is right now:

//CLASS: gladeye.java.jme.audio.AudioManager

package gladeye.java.jme.audio;

import com.jmex.audio.AudioSystem;
import com.jmex.audio.AudioTrack;
import com.jmex.audio.MusicTrackQueue;
import com.jmex.audio.MusicTrackQueue.RepeatType;
import java.net.URL;
import java.io.File;
import java.util.HashMap;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class AudioManager {

    private static HashMap _tracks = new HashMap();
    private static HashMap _trackIndices = new HashMap();
    private static Integer _trackCounter = 0;
    private static AudioTrack _currentTrack = null;
    private static Timer _fadeoutTimer;// = new Timer();
    private static boolean _isFading = false;
    private static float _volume = 1.0f;
    private static ActionListener _fadeoutActionListener;
    private static float _fadeoutTime;
    private static int _fadeoutUpdateRateMilliseconds = 80;
   
   
    public AudioManager()
    {
      System.out.println("AudioManager");
      initFade();
    }

    private static void initFade()
    {
       //create the action listener
         _fadeoutActionListener = new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                updateFadeOut();
             }
           };   
          
           //create the timer
           _fadeoutTimer = new Timer(_fadeoutUpdateRateMilliseconds, _fadeoutActionListener);
    }
   
    public static void addTrack( String key , String trackURL)
    {
   try {
       URL testWav = new File( trackURL ).toURI().toURL();
       AudioTrack track = AudioSystem.getSystem().createAudioTrack(testWav, true);
       _tracks.put(key, track);
       _trackIndices.put(key, _trackCounter);

   } catch (Exception e) {
       e.printStackTrace();
   }
    }

    public static void playTrack(String key)
    {  
       
       cancelFadeOut();
       
       AudioTrack track = (AudioTrack) _tracks.get(key);
       
       if(track == null) {
          System.out.println("track was null" );
          return;
       }
       
       System.out.println("playtrack " + track );
       
       if(_currentTrack == track) return; //dont restart if already playing   

       stop();
      
      if(track != null){
          track.play();
          track.setLooping(true);
          _currentTrack = track;
      }

       AudioSystem audioSystem = AudioSystem.getSystem();
       if(audioSystem != null) audioSystem.setMasterGain(1.0f);
      
    }

    public static boolean trackIsPlaying(String key) //doesnt work
    {
        AudioTrack track = (AudioTrack) _tracks.get(key);
        return track.isPlaying();
    }

   public static void stop()
   {
         if(_currentTrack != null) {
       _currentTrack.stop();
         }
         _currentTrack = null;
   }
  
  
   public static void fadeOut(float time)
   {
      _fadeoutTime = time;
       _isFading = true;
      _fadeoutTimer.start(); 
   }
  
   private static void updateFadeOut()
   {
      if(!_isFading) {
         System.out.println("update fade called when not fading!");
         return;
      }
      if(!AudioSystem.isCreated()) return;//in case is called after program is shutting down
     
      AudioSystem audioSystem = AudioSystem.getSystem();
     
      _volume -=  ((1.0f / _fadeoutTime) / 1000) * _fadeoutUpdateRateMilliseconds;
      //System.out.println("updateFadeOut " + _volume);
     
      if(_volume < 0.0f) {      
         stop();
         if(audioSystem != null) audioSystem.setMasterGain(1f);
         _volume = 1f;
         cancelFadeOut();
         return;
      }
      audioSystem.setMasterGain(_volume);
   }
  
   public static void cancelFadeOut()
   {
      System.out.println("cancelFadeOut()");
     
      _isFading = false;
      _fadeoutTimer.stop();
      if(!AudioSystem.isCreated()) return;     
      AudioSystem audioSystem = AudioSystem.getSystem();
      _volume = 1.0f;
      if(audioSystem != null) audioSystem.setMasterGain(1.0f);

   }

   public static float getVolume()
   {
      return _volume;
   }
  
}