p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   Java Basics (http://p2p.wrox.com/java-basics-199/)
-   -   Adding sound to a java application (http://p2p.wrox.com/java-basics/38368-adding-sound-java-application.html)

john111 February 12th, 2006 01:11 PM

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.

longjava February 12th, 2006 05:36 PM

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.

RahulSapkal February 15th, 2006 05:31 PM

Look at these references,

Playing sound in Java Applet,


Hope this helps,


All times are GMT -4. The time now is 08:34 AM.

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