Problems with JME Audio

I am working on a project with a couple of friends for school. We use the JME AudioSystem, AudioTrack, MusicTrackQueue, and MusicTrackQueue.RepeatType classes to create audio playback for background music in our game. I have three questions about these classes.

  1. How can we sync the beginning of playback with the beginning of the battle?
  2. We are having a problem where we can’t stop music playback after a battle ends. This is really bad, because if another stage is selected, you have two audio tracks playing simultaneously, which introduces lag. We are also attempting to lower the volume of the audio when the game is paused. This also does not work. How do we fix these problems?
  3. Similar to the music tracks in many video games, for each song for a stage, we have an intro part and the main part which is repeated. The way we currently implement this is by having two separate music files and adding them both to the queue. Then when the second track starts playing, the first intro track is removed from the queue and it is set to repeat. For some strange reason, the second track always starts louder than the last. Is there a fix for this?



    Here is the code:

    [java]public class BgmPlayer {



    private static String baseURL = “com/googlecode/reaxion/resources/audio/tracks/”;



    private static String filename;

    private static String ext;

    private static MusicTrackQueue queue;

    private static AudioTrack intro;

    private static AudioTrack track;

    private static boolean hasBGM = false;



    /**
  • Prepares the queue to load sounds.

    */

    public static void prepare() {

    queue = AudioSystem.getSystem().getMusicQueue();

    queue.setCrossfadeinTime(0);

    queue.setCrossfadeoutTime(0);

    queue.setRepeatType(RepeatType.ALL);

    queue.addCurrentSongChangeListener(new SongChangeListener());

    }



    /**
  • Plays and loops a background music track. If a file with “-intro” exists,
  • it will be loaded first and play on the first playback.

    *
  • @param f
  •        Filename of track and track-intro file<br />
    

*/

public static void play(String f) {

hasBGM = true;

stopAndReset();



filename = f.substring(0, f.length() - 4);

ext = f.substring(f.length() - 4);



intro = AudioSystem.getSystem().createAudioTrack(

BgmPlayer.class.getClassLoader().getResource(

baseURL + filename + “-intro” + ext), false);



track = AudioSystem.getSystem().createAudioTrack(

BgmPlayer.class.getClassLoader().getResource(

baseURL + filename + ext), false);



queue.addTrack(intro);

queue.addTrack(track);



queue.play();

}



/**

  • Clears all tracks from the queue and frees them from memory.

    */

    public static void stopAndReset() {

    if (hasBGM) {

    queue.stop();

    ArrayList tracks = queue.getTrackList();

    for (int i = 0; i < tracks.size(); i++) {

    queue.getTrack(i).stop();

    queue.getTrack(i).release();

    }

    queue.clearTracks();

    hasBGM = false;

    }

    }



    public static void gamePaused() {

    if (hasBGM)

    queue.getCurrentTrack().setTargetVolume(0.4f);

    }



    public static void gameUnpaused() {

    if (hasBGM)

    queue.getCurrentTrack().setTargetVolume(1);

    }



    private static class SongChangeListener implements ChangeListener {

    public void stateChanged(ChangeEvent e) {

    AudioSystem.getSystem().getMusicQueue().removeTrack(intro);

    }

    }



    }[/java]

Note: The import statements are missing at the top because i didn’t copy them. lol.