• 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

How to stop a audio file when it starts playing?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 242
27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 242
27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic