Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Display Modes
  #1 (permalink)  
Old February 12th, 2006, 01:11 PM
Registered User
Join Date: Feb 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding sound to a java application

Hi, Can anybody explain to me how i can add sound to a stock control system. For example if somebody presses the logout button i want to play a sound. All help will be greatly appreciated. Thanks.

Reply With Quote
  #2 (permalink)  
Old February 12th, 2006, 05:36 PM
Authorized User
Join Date: Jan 2006
Location: , , USA.
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to longjava

There are two ways you can play a sound file:

The simple way is to use a static method newAudioClip() of an Applet class to get an instance of AudioClip, then invoke the play method.
For example:
JButton logout = new JButton( "Logout" );
logout.addActionListener( new ActionListener()
   public void actionPerformed( ActionEvent evt )
      // play a sound before loging out
         java.applet.AudioClip clip =
            java.applet.Applet.newAudioClip(new java.net.URL( "file:/logout.wav"));

         // loging out.....
       catch( Exception e ) {}

The alternative way, which is a right way to do, is using the Java Sound API. You need to learn the following classes of Java Sound API in order to play a sound file successfully:
- AudioInputStream
- AudioFormat
- DataLine
- SourceDataLine

Hope this help.

Reply With Quote
  #3 (permalink)  
Old February 15th, 2006, 05:31 PM
Authorized User
Join Date: Aug 2005
Location: Chelmsford, MA, USA.
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts

Look at these references,

Playing sound in Java Applet,


Hope this helps,

Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
playing sound files (java 2 jdk1.3 edition) naresh Java Basics 1 April 11th, 2009 01:04 AM
Adding AJAX to existing ASP.NET application lols BOOK: Beginning Ajax with ASP.NET 1 January 29th, 2007 09:25 PM
[JAVA] PRINT - SOUND gianni17 J2EE 0 February 20th, 2005 12:11 PM
Java Application jstatum Pro JSP 3 September 20th, 2004 02:41 AM
Java Sound Help anjithalb BOOK: Beginning Java 2 0 September 18th, 2003 02:42 AM

All times are GMT -4. The time now is 06:05 AM.

Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.