Java examples for javax.sound.midi:Sequence
Play a MIDI sequence.
//package com.java2s; import javax.sound.midi.Sequencer; import javax.sound.midi.Sequence; import javax.sound.midi.MidiSystem; public class Main { static Sequencer sequencer; /** /*w w w .j ava2s. com*/ * Play a sequence. Stops playing any currently playing sequences. */ public static void play(Sequence sequence) { try { // if the sequencer hasn't been initialized, // initialize it if (sequencer == null) { sequencer = MidiSystem.getSequencer(); } // if the sequencer hasn't been opened, open it for playing if (!sequencer.isOpen()) { sequencer.open(); } // if the sequencer is running, we need to stop it if (sequencer.isRunning()) { sequencer.stop(); } // rewind it to the beginning and start playing again sequencer.setTickPosition(0); sequencer.setSequence(sequence); sequencer.start(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }