0

So, im making a midi system for e 2-d java game on developing and so far i was able to make a simple midi file that plays + stops, but i've been trying to play with volume and fading but it doesn't seem to want to work right.

Here's my class file:

package sign;
import java.io.*;
import javax.sound.midi.*;

public class Midi {

    private static Sequencer sequencer = null;  

    public void play(String filename) {
        play(filename,false); 
        }

    public static void play(String filename, boolean loop) {
        try {
            stop();
            sequencer = MidiSystem.getSequencer();
            File midiFile = new File(filename);
            sequencer.setSequence(MidiSystem.getSequence(midiFile));
            if (loop)
                sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
            sequencer.open();
            sequencer.start();
         } catch (Exception e) {
            System.err.println("MidiPlayer: " + e);
            sequencer = null;
         }
    }

    public static Sequencer getSequencer() { 
        return sequencer; 
        }

    public static void stop() {
        try {
            if ((sequencer == null) || (!sequencer.isRunning())) return;
            sequencer.stop();
            sequencer.close();
        }
        catch (Exception e) {
            System.err.println("MidiPlayer: " + e);
        }
        sequencer = null;
    }

}

What i'm asking is, can anyone give me a explanation on how to impliment volume + fading with this file?.

I've tried some methods of fading + volume control but they always crash my game because they require some ridiculously older midi device for it to work.

Beau Grantham
  • 3,435
  • 5
  • 33
  • 43
Garrett
  • 9
  • 2

1 Answers1

1

How to change the volume of a Sequencer has been answered here: What's the method to control volume in an MIDI sequencer?

For fading, you'll have to implement that yourself (i.e. periodically lower the volume of the sequencer).

Community
  • 1
  • 1
Thalur
  • 1,155
  • 9
  • 13