javax.sound.midi


  1. Playing a MIDI file:

    import javax.sound.midi.*;
    
    public class MidiPlayer {
    
        public static void main(String[] args) {
            try {
                // Create a Sequencer
                Sequencer sequencer = MidiSystem.getSequencer();
                // Open the Sequencer
                sequencer.open();
                // Load a MIDI file into the Sequencer
                sequencer.setSequence(MidiSystem.getSequence(new File("mySong.mid")));
                // Start the Sequencer
                sequencer.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  2. Recording a MIDI performance:

    import javax.sound.midi.*;
    
    public class MidiRecorder {
    
        public static void main(String[] args) {
            try {
                // Create a Sequencer
                Sequencer sequencer = MidiSystem.getSequencer();
                // Open the Sequencer
                sequencer.open();
                // Create a new Sequence
                Sequence sequence = new Sequence(Sequence.PPQ, 24);
                // Create a new Track
                Track track = sequence.createTrack();
                // Record MIDI events to the Track
                sequencer.recordEnable(track, -1);
                // Start the Sequencer
                sequencer.start();
                // Stop the Sequencer after 10 seconds
                Thread.sleep(10000);
                // Stop recording MIDI events
                sequencer.recordDisable(track);
                // Save the Sequence to a MIDI file
                MidiSystem.write(sequence, 1, new File("myRecording.mid"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  3. Creating a simple synthesizer:

    import javax.sound.midi.*;
    
    public class SimpleSynthesizer {
    
        public static void main(String[] args) {
            try {
                // Create a Synthesizer
                Synthesizer synthesizer = MidiSystem.getSynthesizer();
                // Open the Synthesizer
                synthesizer.open();
                // Load a sound bank into the Synthesizer
                synthesizer.loadInstrument(synthesizer.getDefaultSoundbank().getInstruments()[0]);
                // Create a new Channel
                Channel channel = synthesizer.getChannels()[0];
                // Play a note
                channel.noteOn(60, 100);
                // Hold the note for 1 second
                Thread.sleep(1000);
                // Release the note
                channel.noteOff(60);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  4. Creating a custom MIDI controller:

    import javax.sound.midi.*;
    
    public class MidiController {
    
        public static void main(String[] args) {
            try {
                // Create a MIDI device
                MidiDevice device = MidiSystem.getMidiDevice(0);
                // Open the MIDI device
                device.open();
                // Create a MIDI transmitter
                Transmitter transmitter = device.getTransmitter();
                // Create a MIDI receiver