Reputation: 462
I'm trying to loop a MIDI sequence in a java game I'm making and I'm having some issues.
The current code I have does repeat the sequence, but there is a large delay between the end of the sequence and the restart. How can I eliminate this?
Here's my code:
try
{
// From file
final Sequence sequence = MidiSystem.getSequence(new File("main menu.mid"));
sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.addMetaEventListener(new MetaEventListener() {
public void meta(MetaMessage msg) {
if (msg.getType() == 47) { // End of track
sequencer.setTickPosition(0);
try
{
sequencer.setSequence(sequence);
} catch(InvalidMidiDataException e) {e.printStackTrace();}
sequencer.start();
}
}
});
sequencer.setSequence(sequence);
// Start playing
sequencer.start();
} catch (IOException e) {e.printStackTrace();}
catch (MidiUnavailableException e) {e.printStackTrace();}
catch (InvalidMidiDataException e) {e.printStackTrace();}
Upvotes: 4
Views: 1268
Reputation: 168795
This source adapted from the Java Sound tag Wiki plays the MIDI without a 'long delay' between loops, which suggests to me that the delay you are hearing is part of the silent intro/outtro of the existing track.
import javax.sound.midi.*;
import javax.swing.JOptionPane;
import java.net.URL;
class LoopMidi {
public static void main(String[] args) throws Exception {
URL url = new URL("http://pscode.org/media/EverLove.mid");
Sequence sequence = MidiSystem.getSequence(url);
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.setSequence(sequence);
//sequencer.setLoopStartPoint(2000);
//sequencer.setLoopEndPoint(4000);
sequencer.setLoopCount(5);
sequencer.start();
JOptionPane.showMessageDialog(null, "Everlasting Love");
}
}
The solution then lies in either:
Upvotes: 2