09 January, 2008

Simple Java MIDI player sample

New Year!

XenoHarmonica
- is a musical project. It is a bayan keyboard emulator, Java MIDI application for personal computers.
Now everybody can play bayan (button accordion).

XenoHarmonica is free for education and non-commercial usage.

Program based on Java MIDI API.
In this article I provide an example of simplest MIDI player.

Maybe somebody else going to create MIDI program :-)



MidiPlayerSample.java
package xantorohara.xenoharmonica.samples;
 
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import java.io.FileInputStream;
 
public class MidiPlayerSample {
 
    public static void main(String[] args) {
        try {
            Sequencer sequencer = MidiSystem.getSequencer();
            if (sequencer == null)
                throw new MidiUnavailableException();
            sequencer.open();
            FileInputStream is = new FileInputStream("sample.mid");
            Sequence mySeq = MidiSystem.getSequence(is);
            sequencer.setSequence(mySeq);
            sequencer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 

1 comment:

Anonymous said...

I would also like to know how to not use sequencer.start() and instead employ the various Java methods like
sequencer.getTracks()[track].get((int)event).getTick(); ... etc., so as to "drive" a keyboard. Meta events are difficult for me as of now.
Could you advice?


Nice ideas and site.
Thanks.