• 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

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

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That makes a lot of sense (except why they didn't mention it) and a simple solution.

Thanks!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
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