• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why doesn't my progam quit?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 56
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
They weren't very bright, but they were very, very big. Ad contrast:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic