• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why doesn't my progam quit?

 
Oliver Stuttgart
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,

I absolutely can't find the reason why my program doesn't quit.
It is one of the examples in the book 'Head First Java'.

This is 'my' code:



Everything seems to work fine, it plays the sound as expected and obviously also jumps back to main, because the terminal also shows the message which I have entered for debugging, 'System.out.println("after mini.play");' at the end of main.
But then - the program never quits and the terminal stands there waiting forever.

The problem occurs on my Ubuntu Linux computer as well as on another one with XP on it. For this reason I think that I made a mistake which I am simply not able to find.

What did I do wrong?

Cheers,

Oliver
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to close() the Sequencer. Without that it still has a running thread somewhere. The issue is when to close it, though. You should wait for an event that signals the playing is done. Now that's usually where ControllerEventListener is for, but my tests haven't been able to get it to really listen.
 
Misha van Tol
Ranch Hand
Posts: 56
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the isRunning() method on player to check when it's done.
 
Oliver Stuttgart
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Thank you so much for your replies. Without them I never would have found the error.
They really should have mentioned in the book that this behaviour will occur - I double checked the code in the book, but there is definitely no close() method used.

Well - now it works, even so my solution is probably bad style!?



Btw.: while browsing through the API my first idea also was to use the isRunning() method, but that didn't work. I guess that it stays true until the sequencer's close() method is called what will never happen in this case.
That's how I used it:



Cheers,

Oliver
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But that would only work if you would poll the sequencer from time to time. Now I did notice that a meta event is also trigger. So the code with MetaEventListener:
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oliver, isRunning() will return false before close() is called; my code proves that. It is at least a bit better because it doesn't use busy waiting.
 
Oliver Stuttgart
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Rob,

thanks for your help.

Rob Prime wrote:Oliver, isRunning() will return false before close() is called; my code proves that. It is at least a bit better because it doesn't use busy waiting.


I was expecting something like this, that's why I guessed that my solution is probably 'bad style'....

I don't yet exactly understand how this listener thing works, but you gave me an idea of it. Later in the book they'll also take care of listeners.

Cheers,

Oliver
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic