Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

MiniMusicPlayer1 from Head First Java 2nd Edition

843802Apr 20 2010 — edited Apr 26 2010
import javax.sound.midi.*;

public class MiniMusicPlayer1 {

	public static void main(String[] args) {

		try {

			Sequencer sequencer = MidiSystem.getSequencer();
			sequencer.open();

			Sequence seq = new Sequence(Sequence.PPQ, 4);
			Track track = seq.createTrack();

			for(int i = 5; i < 61; i+= 4) {
				
				track.add(makeEvent(144,1,i,100,i));
				track.add(makeEvent(128,1,i,100,i+2));
			}

		sequencer.setSequence(seq);
		sequencer.setTempoInBPM(220);
		sequencer.start();
		} catch(Exception e) { e.printStackTrace(); }
	}

	public static MidiEvent makeEvent(int comd, int chan, int one, int two, int tick) {

		MidiEvent event = null;
		try {
			
			ShortMessage a = new ShortMessage();
			a.setMessage(comd, chan, one, two);
			event = new MidiEvent(a, tick);
		} catch (Exception e) { e.printStackTrace(); }

		return event;
	}
}
I can compile and run the program. But this does not terminates automatically. I have to use ctrl+c. Please help to find what goes wrong??
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 24 2010
Added on Apr 20 2010
4 comments
183 views