How to Make a MIDI Loop in Java
The MIDI audio format sends an electronic signal that is handled by a sequencer. The Java language includes methods and classes specifically designed to control MIDI files and sequencers. Repeating or looping a MIDI file requires the Loop Continuously value in a sequencer method.
Instructions
-
-
1
Add the following import code to the beginning of your Java program:
import javax.sound.midi.*;
import java.io.*; -
2
Create a MidiPlayer class in your program:
class MidiPlayer {
} -
-
3
Place the following code between the opening and closing brackets of the MidiPlayer class:
public static void play(String filename) {
} -
4
Insert the following code between the opening and closing brackets of the play object:
sequencer = MidiSystem.getSequencer();
File midiFile = new File(filename);
sequencer.setSequence(MidiSystem.getSequence(midiFile));
sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
sequencer.open();
sequencer.start(); -
5
Play the MIDI file:
MidiPlayer.play(midiFilename);
Replace "midiFilename" with the name of the file that you want to play.
-
1