Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

javax.sound.midi (A Head First Java Question)  RSS feed

 
Tj Kendon
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm playing along with the Head First Java book and I'm working on the midi examples in chapter 11.

For those of you not playing along at home there are a number of examples but they're all basically of the form :

/**
*
* Example class from Head First Java.
* Sierra, Kathy & Bates, Bert 2nd Ed (2005) O O'Riley Medi
*
*/

import javax.sound.midi.*;

public class MiniMiniMusicApp {

public static void main (String[] args) {

MiniMiniMusicApp mini = new MiniMiniMusicApp();
mini.play();

}


public void play() {

try {

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

Sequence seq = new Sequence(Sequence.PPQ, 4);

Track track = seq.createTrack();

ShortMessage a = new ShortMessage();
a.setMessage(144, 1, 44, 100);
MidiEvent noteOn = new MidiEvent(a, 1);
track.add(noteOn);

ShortMessage b = new ShortMessage();
b.setMessage(128, 1, 44, 100);
MidiEvent noteOff = new MidiEvent(b, 16);
track.add(noteOff);

player.setSequence(seq);
player.start();

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

Now the program works okay, makes noise and every thing, quite happily, but it hangs at the end.

Debugging it looks like it goes through all the midi code, but then waits at the last line of the program. I'm not sure I haven't spent that much time debugging.

Any ideas? I've tried setting the mini reference in main to null, but that doesn't seem to help, and calling play.stop() (which seems like it might make sense) only causes it to play nothing at all.

I realize this question has been asked before, but it doesn't seem to have been answered.

Thank you.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This doesn't really answer your question, but you can add an event listener that gets notified when the sequence is done playing.
 
Richard Long
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran into the same thing when I was trying that example in the book. I added the following lines after the player.start(); statement to fix the problem.



If you look at the java api documentation http://java.sun.com/javase/6/docs/api/javax/sound/midi/MidiDevice.html#open() you will see that you need to call the close() method to release system resources and exit the application cleanly. Not sure why this is not part of the example in the book.
 
Tj Kendon
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes a lot of sense (except why they didn't mention it) and a simple solution.

Thanks!
 
Robbie Hinman
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not to kick a dead horse, but I was fiddling with that issue too and it was a little frustrating. Might be worth noting in the 3rd edition.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!