• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

MiniMiniMusicApp? anyone else having issues with it?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Glen Jaravanch," please check your private messages by clicking on My Private Messages. Thanks!
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!


 
Would you like to try a free sample? Today we are featuring tiny ads:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic