Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 12th, 2006, 02:11 PM
Registered User
Join Date: Feb 2006
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.

Old February 12th, 2006, 06:36 PM
Authorized User
Join Date: Jan 2006
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.

Old February 15th, 2006, 06:31 PM
Authorized User
Join Date: Aug 2005
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts

Look at these references,

Playing sound in Java Applet,


Hope this helps,


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 10:25 PM
[JAVA] PRINT - SOUND gianni17 J2EE 0 February 20th, 2005 01: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

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.