I have created the sample MIDI app in the headstart book, page 342. I am not sure if I should copy the source here, for copyright reasons and whatnot, but I have triple-checked to see that the code I entered is identical to what's in the book.
Basically, a method is invoked that does the following:
1) create a Sequencer 2) create a Sequence 3) create a Track in the Sequence 4) Add two events to the track 5) associate the sequence to the sequencer 6) invoke the start() method on the Sequencer.
The note plays, and I can change the pitch, duration, etc. But from the command-line, control never returns. I have to eventually press Ctrl-C... I added a System.exit(1); after the start() invocation, but then the command line returns before the sound plays.
Now, maybe I am getting ahead of myself (GUIs are in the next chapter), but after the note plays, the sequence is done, why won't the program exit?
Well, Sequencer has a stop() method - have you tried calling that? Of course you can't call it immediately or it will stop before you've played anything. Perhaps a call to Thread.sleep(10000) (ten seconds) followed by a call to stop()? When you get to the GUI section you'll probably cover how to call stop() in response to a user command instead. Until then, maybe using control-C is your best bet.
I don't know what this "headstart" book is - perhaps you mean Head First Java? If so perhaps this question is better answered by someone who has a copy of the book, and thus a better perspective on what the code is doing here.
"I'm not back." - Bill Harding, Twister
Joined: Dec 05, 2005
hee hee... I meant Head First... not Head Start...
That did it. Actually, waiting ten seconds and invoking the stop method did not return the command line, but waiting ten seconds then issuing a System.exit(1) returns control.
I peeked ahead a bit in the book, and it looks like this will be covered later. Part of my problem is that I come from the procedural side of the house. I have been programming in procedural languages for years. I am still struggling to write in 'object-oriented' mode, but I will get there.