• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Head First Java Ch 11 Example

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried typing and compiling the example in ch 11 of Head First Java, which is a program that plays a note. When I run it, the command window frezes up as if the program won't stop running. Then I have to shut down the command window, start it all over again, and I don't think the program is supposed to do that. Here is the code:


This is pretty cool stuff, and I wish I didn't have to restart the command window, re-type in the directory, etc jsut to run the program! Is there any way to get around this? The program itself is pretty much copied word for word from the book.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you learnt while loops yet?
Using a while loop, you can ask the user if he wants more fun or should the application quit.
 
Benjamin Thvedt
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do know about while loops. I'm not sure how I would use one here though. The problem is that the command line window is freezing. If the command line window did not freeze, then I could put everything in a loop no problem. The program should play a note then terminate. Instead, it plays a note then freezes up. Even if I get rid of the player.start() line, it won't play a note, but it still freezes the command window, as if the program is not terminating somehow. Which leaves me to believe that it's not a problem with midi or anything like that, it's probabily just something in the code that I am not catching.
 
Benjamin Thvedt
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I have messed around some more, and the problem is really that the program won't terminate when it is done. Everything else works. Does anyone have any insight as to why the program won't terminate? Here is my code so far:
As you can see, it just gets input from the user which is 2 numbers, then it calls the method play which plays the note with the given instrument, and it just keeps doing that until the user enters "Q" at the right time.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

I hope this is still of some use to somebody?

try adding

to the end of the play method.

I'm looking at the same tutorial and was trying to figure out why i get no sound output. I get no compilation or runtime errors, but no sound is produced.
I'm guessing this might be machine specifc, but not sure where to start looking?
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

try adding

player.close();

to the end of the play method.


I've never used the sound stuff so I may be wrong but I would have thought it played sounds asynchronously in which case that would stop the player immediately, probably before it's even played any sound.
Calling player.close() once "Q" is pressed would be a good idea though.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic