play Midi - Java javax.sound.midi

Java examples for javax.sound.midi:MidiSystem

Description

play Midi

Demo Code


//package com.java2s;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;

public class Main {
    private static Sequencer sequencer;

    public static void playMidi(Sequence midi) {
        try {/*from   w w w .  jav a2 s.  c o  m*/
            if (sequencer == null)
                sequencer = MidiSystem.getSequencer();
            if (sequencer.isOpen()) {
                sequencer.stop();
                sequencer.setTickPosition(0);
            }

            if (midi == null) {
                sequencer.stop();
                sequencer.close();
                return;
            }

            sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
            //sequencer.setLoopCount(0);
            sequencer.setSequence(midi);
            sequencer.open();

            sequencer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials