File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MiniMiniMusicApp? anyone else having issues with it?

 
Glen Lipman
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Glen Jaravanch," please check your private messages by clicking on My Private Messages. Thanks!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the exact code you're using and let us know what operating system you're running?
 
Glen Lipman
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic