This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes MiniMiniMusicApp?  anyone else having issues with it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "MiniMiniMusicApp?  anyone else having issues with it?" Watch "MiniMiniMusicApp?  anyone else having issues with it?" New topic
Author

MiniMiniMusicApp? anyone else having issues with it?

Glen Lipman
Greenhorn

Joined: Feb 18, 2008
Posts: 3
MiniMiniMusicApp - from the HEad first Java book - the class compiles but when I run it nothing happens. I don't hear a music note being played and the command prompt just hangs. Anyone have further info on this?

Also, on a side note this forum runs extremely slowwwwww.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

"Glen Jaravanch," please check your private messages by clicking on My Private Messages. Thanks!


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Can you post the exact code you're using and let us know what operating system you're running?
Glen Lipman
Greenhorn

Joined: Feb 18, 2008
Posts: 3
Here is the code (exactly as it appears in the book)
using windows XP

import javax.sound.midi.*;


public class MiniMiniMusicApp { // this is the first one

public static void main(String[] args) {
MiniMiniMusicApp mini = new MiniMiniMusicApp();
mini.play();
}

public void play() {

try {

// make (and open) a sequencer, make a sequence and track

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

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

// now make two midi events (containing a midi message)
MidiEvent event = null;

// first make the message
// then stick the message into a midi event
// and add the event to the track

ShortMessage a = new ShortMessage();
a.setMessage(144, 1, 44, 100);
MidiEvent noteOn = new MidiEvent(a, 1); // <-- means at tick one, the above event happens
track.add(noteOn);

ShortMessage b = new ShortMessage();
b.setMessage(128, 1, 44, 100);
MidiEvent noteOff = new MidiEvent(b, 16); // <-- means at tick one, the above event happens
track.add(noteOff);

// add the events to the track

// add the sequence to the sequencer, set timing, and start
sequencer.setSequence(seq);

sequencer.start();

} catch (Exception ex) {ex.printStackTrace();}
} // close play

} // close class
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The hanging command prompt can be fixed by adding the following 3 lines immediately after calling sequencer.start()...

This will cause a 2-second pause (which should be long enough for the sequencer to play the note), after which it will close the Sequencer and exit the program. There might be a "better" way, but this should work.

As for why there's no sound, I'm not sure. The code works for me (on a Mac). I know this seems lame, but are you sure your volume is turned up?
Glen Lipman
Greenhorn

Joined: Feb 18, 2008
Posts: 3
Originally posted by marc weber:
The hanging command prompt can be fixed by adding the following 3 lines immediately after calling sequencer.start()...

This will cause a 2-second pause (which should be long enough for the sequencer to play the note), after which it will close the Sequencer and exit the program. There might be a "better" way, but this should work.

As for why there's no sound, I'm not sure. The code works for me (on a Mac). I know this seems lame, but are you sure your volume is turned up?


Thank you sir. That Thread.sleep call did the trick. As for the sound, I had to play around with the MIDI settings in the control panel and then it worked!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Glen Lipman:
...I had to play around with the MIDI settings in the control panel and then it worked!

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MiniMiniMusicApp? anyone else having issues with it?
 
Similar Threads
Head First Java Chapter 11 MiniMiniMusicApp
MiniMiniMusicApp from Head First Java
Head First : MiniMiniMusicApp
Application doesn't quit -- MiniMiniMusicApp sample from Head First Java
Head First Java chapter 11 MiniMiniMusicApp