Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to stop a audio file when it starts playing?  RSS feed

 
Edward Richtofen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have a question on how to do something in Java. How can I get a audio file to stop playing once it starts playing in Java? In my current code I have it starts playing when a variable equals 1 and I want to stop when it equals 2. Here is what I have so far



so how can I get it to stop playing when count == 2? Thanks.
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this code will stop the sound:

So, you can put it in an if statement when count==2.

Also, it's bad practice to catch Exception, and worse practice to let it silently fail (do nothing in the catch). And I suggest you use lambda expressions instead of a class that implements ActionListener.
 
Edward Richtofen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zachary Griggs wrote:Well, this code will stop the sound:

So, you can put it in an if statement when count==2.

Also, it's bad practice to catch Exception, and worse practice to let it silently fail (do nothing in the catch). And I suggest you use lambda expressions instead of a class that implements ActionListener.


Thanks for the help but I am still having some trouble. If I put it on any line after the catch I get an error saying it does recognize audioStream. If I put it before the catch it compiles but does not actually stop the audio. Here is my new code



how can I fix this?
 
Paul Clapham
Sheriff
Posts: 22489
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zachary Griggs wrote:Also, it's bad practice to catch Exception, and worse practice to let it silently fail (do nothing in the catch).


You're still ignoring exceptions. So you're calling the stop() method, which is known to throw exceptions if something goes wrong. And it appears that something may be going wrong. If that's the case then your empty catch block is executed, telling you exactly nothing. So stop ignoring the exceptions. At the very least print out the stack trace:

 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of the reasons why I think checked exceptions were a poor design choice on Java's part. They encourage catching and forgetting to make your program compile, while letting the exception go through fully would actually give more info.

One thing I've noticed: all of this code you're writing is in the if(count==1) conditional. Since you're incrementing this, it will be true only one time (when count==1), next time you run it nothing will happen (since count=2, count!=1). I'm almost sure this is a problem, though not sure if it's the only one. Also, the getSource() method is evil. It encourages you to dump all your event handling code into one mega method and then check for each button one-by-one:

Lambda expressions are easier to write and easier to read.
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zachary Griggs wrote:. . . Also, the getSource() method is evil. . . .
Yes, it is, isn't it. It actually tempts you into the worst excesses of non‑object‑oriented design.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!